Post data and retrieve the response using PHP Curl?
You'll have to set the CURLOPT_RETURNTRANSFER option to true.
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
The response to your request will be available in the $result variable.
How to Retrieve cURL Data within the URL Endpoint
You can access to these data via the $_SERVER
superglobal.
Endpoint :
<?php
$user = $_SERVER['PHP_AUTH_USER']; // alex
$password = $_SERVER['PHP_AUTH_PW']; // password123
php curl post request and get result response
The very first: I'm using Firebug
add-on and see what happen
Here one ajax get content from other site
We have this link
https://apis.pos.com.my/apigateway/as2corporate/api/v2trackntracewebapijson/v1/?id=EP024922993MY&Culture=En
In request of header we can see require X-User-Key
Now we must find X-User-Key => We can view source
Now we build source code
<?php
function _curl($url,$post="",$usecookie = false,$_sock = false,$timeout = false,$x_user_key = false) {
$ch = curl_init();
if($post) {
curl_setopt($ch, CURLOPT_POST ,1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $post);
}
if($timeout){
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,$timeout);
}
if($_sock){
curl_setopt($ch, CURLOPT_PROXY, $_sock);
curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.16");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'X-Apple-Tz: 0',
'X-Apple-Store-Front: 143444,12'
));
if($x_user_key){
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'X-User-Key: '.$x_user_key,
'Referer: http://poslaju.com.my/track-trace/',
'Origin: http://poslaju.com.my'
));
}
if ($usecookie) {
curl_setopt($ch, CURLOPT_COOKIEJAR, $usecookie);
curl_setopt($ch, CURLOPT_COOKIEFILE, $usecookie);
}
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result=curl_exec ($ch);
curl_close ($ch);
return $result;
}
function getStr($string,$start,$end){
$str = explode($start,$string,2);
$str = explode($end,$str[1],2);
return $str[0];
}
$url = 'http://poslaju.com.my/track-trace/';
$result_curl = _curl($url,'','','','','');
$x_user_key = getStr($result_curl,'{ "X-User-Key": "','" }');
$id_track = 'EP024922993MY';
$url = 'https://apis.pos.com.my/apigateway/as2corporate/api/v2trackntracewebapijson/v1/?id='.$id_track.'&Culture=En';
$result_curl = _curl($url,'','','','',$x_user_key);
echo $result_curl;
?>
Change id
on your mind
You can get json
content
Use print_r(json_decode($content_you_got)
And result you will have like this
How to get response using cURL in PHP
Just use the below piece of code to get the response from restful web service url, I use social mention url.
$response = get_web_page("http://socialmention.com/search?q=iphone+apps&f=json&t=microblogs&lang=fr");
$resArr = array();
$resArr = json_decode($response);
echo "<pre>"; print_r($resArr); echo "</pre>";
function get_web_page($url) {
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
CURLOPT_ENCODING => "", // handle compressed
CURLOPT_USERAGENT => "test", // name of client
CURLOPT_AUTOREFERER => true, // set referrer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // time-out on connect
CURLOPT_TIMEOUT => 120, // time-out on response
);
$ch = curl_init($url);
curl_setopt_array($ch, $options);
$content = curl_exec($ch);
curl_close($ch);
return $content;
}
PHP, cURL, and HTTP POST example?
<?php
//
// A very simple PHP example that sends a HTTP POST to a remote site
//
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://www.example.com/tester.phtml");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
"postvar1=value1&postvar2=value2&postvar3=value3");
// In real life you should use something like:
// curl_setopt($ch, CURLOPT_POSTFIELDS,
// http_build_query(array('postvar1' => 'value1')));
// Receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
curl_close($ch);
// Further processing ...
if ($server_output == "OK") { ... } else { ... }
?>
How to post data using curl and get reponse based on posted data
The function on this link will work.
<?php
function post_to_url($url, $data) {
$fields = '';
foreach ($data as $key => $value) {
$fields .= $key . '=' . $value . '&';
}
rtrim($fields, '&');
$post = curl_init();
curl_setopt($post, CURLOPT_URL, $url);
curl_setopt($post, CURLOPT_POST, count($data));
curl_setopt($post, CURLOPT_POSTFIELDS, $fields);
curl_setopt($post, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($post);
curl_close($post);
return $result;
}
$data = array(
"name" => "new Name",
"website" => "http://bavotasan.com",
"twitterID" => "bavotasan"
);
$surl = 'http://mydomain.com/curl.php';
echo post_to_url($surl, $data);
?>
Whereas, on curl.php
<?php
file_put_contents('abc.text', "here is my data " . implode('->', $_POST));
if ($_POST['name']):
print_r($_POST['name']);
endif;
?>
Retrieve the response code from header cURL php
I think you need to pass $curl
to the curl_getinfo
method, not the $response
$response = curl_exec($curl);
$theInfo = curl_getinfo($curl);
$http_code = $theInfo['http_code'];
You can see the doco here.. https://www.php.net/manual/en/function.curl-getinfo.php
Related Topics
How to Allow Remote Access to My Wamp Server for Mobile(Android)
How to Redirect a 404 Error in a Custom 404 Page Using Codeigniter
PHP Send Email with Attachment
Registry Design Pattern...Good or Bad
Laravel Eloquent Sort by Relationship
Multidimensional Array PHP Implode
How to Check for Special Characters PHP
Inet_Aton() and Inet_Ntoa() in PHP
Laravel Form HTML with Put Method for Put Routes
Return One Value from Database with MySQL PHP Pdo
PHP File-Upload Using Jquery Post
How to Find Annotations in a PHP5 Object
How to Get Jquery Code Completion in Netbeans