I need to capture the request headers and response while using PHP cURL to post data to external API call. The localhost page load shows in traffic where as the PHP cURL is not shown.
$url = "https://https://gds.eligibleapi.com/v1.3/enrollment.json";
$ch = curl_init(); // initialize curl handle
$user_agent = $_SERVER['HTTP_USER_AGENT'];
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt($ch, CURLOPT_URL, $url); // set url to post to
curl_setopt($ch, CURLOPT_FAILONERROR, 1); // Fail on errors
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // allow redirects
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); // add POST fields
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data_string)));
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, 2);
$data = curl_exec($ch); // if($data === false) echo 'Curl error: ' . curl_error($ch);
echo $data;
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
Your URL is malformed (you have https://https://).
You need to set the Proxy option on the CURL command, e.g.
curl_setopt($ch, CURLOPT_PROXY, '127.0.0.1:8888');
See http://fiddler2.com/documentation/Configure-Fiddler/Tasks/ConfigurePHPcURL
You have to configure Fiddler to decrypt HTTPS traffic.
See here http://fiddler2.com/documentation/Configure-Fiddler/Tasks/DecryptHTTPS
Related
I have following code which returns an empty string (in variable $result)
$createdURL = "https://telenorcsms.com.pk:27677/corporate_sms2/api/sendsms.jsp?session_id=123&to=92315&text=test&mask=1"
curl_setopt($ch, CURLOPT_URL, "$createdURL");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml; charset=utf-8')) ;
curl_setopt($ch, CURLOPT_HEADER, 0);
$result = curl_exec($ch);
echo var_dump($result);
if(curl_errno($ch)){
echo 'Curl error: ' . curl_error($ch);
}
curl_close($ch);
Whereas when I run the link given in first line in browser address bar it returns following XML.
<corpsms>
<command>Submit_SM</command>
<data>Error 102</data>
<response>Error</response>
</corpsms>
Please help where am I wrong.
You are missing curl_init()
So use this code and you'll get a response from the API:
<?php
$createdURL = "https://telenorcsms.com.pk:27677/corporate_sms2/api/sendsms.jsp?session_id=123&to=92315&text=test&mask=1";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $createdURL);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml; charset=utf-8')) ;
curl_setopt($ch, CURLOPT_HEADER, 0);
$result = curl_exec($ch);
echo var_dump($result);
if(curl_errno($ch)){
echo 'Curl error: ' . curl_error($ch);
}
curl_close($ch);
You should also check whether you need to send the Content-Tye: text/xml header and whether you should be using a POST or a GET.
This was due to space given in parameters of link like below
$createdURL = "https://telenorcsms.com.pk:27677/corporate_sms2/api/sendsms.jsp?session_id=123&to=92315&text=test and test&mask=my mask";
After removing space it is now giving output, but now need to tackle matter of spaces.
You can enable the CURLOPT_VERBOSE option:
curl_setopt($ch, CURLOPT_VERBOSE, true);
When CURLOPT_VERBOSE is set, output is written to STDERR or the file specified using CURLOPT_STDERR. The output is very informative.
You can also use tcpdump or wireshark to watch the network traffic.
I am using php curl to ping and post data to a url. But it is not working.response of url is "not found" this comes when the posting url is "http://example.com/conversion_get/"
I suspect it was because of .jpg in curl url.
I am using same code in same application to post data. but it works fine. Can any body help me to sort out this issue?
My code:
$kps="6789345607655ABF5k23929ocern46789345607655ABF5k23929ocern4";
$url="http://example.com/conversion_get/pixel.jpg";
$ch = curl_init();
if (!$ch){die("Couldn't initialize a cURL handle");}
$ret = curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt ($ch, CURLOPT_POSTFIELDS, "kp=".$kps);
$ret = curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$curlresponse = curl_exec($ch);
curl_setopt($ch, CURLOPT_HEADER, 0);
if(curl_errno($ch))
echo 'curl error : '. curl_error($ch);
if (empty($ret)) {
curl_close($ch);
} else {
$info = curl_getinfo($ch);
curl_close($ch); // close cURL handler
$ns= $curlresponse;
}
This is PHP cURL code I'm using to maintain HTTP session with cookies
if(!function_exists("\curl_init")){
\load_curl();
echo "curl loaded";
} else {
echo "curl already exists ";
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, ''); //could be empty, but cause problems on some hosts
curl_setopt($ch, CURLOPT_COOKIEFILE, ''); //could be empty, but cause problems
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, $url1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POSTFIELDS, $startInterview);
curl_setopt($ch, CURLOPT_HTTPHEADER, $startInterviewHeaders);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
//curl_setopt($ch, CURLOPT_HEADER, 1);
$startInterviewresponse = curl_exec($ch);
//echo $response;
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curl_errno= curl_errno($ch);
echo "</br> HTTP status: " . $http_status . "</br> cURL error: " .$curl_errno . "</br>";
//curl_close($ch); // close cURL
echo $startInterviewresponse;
$investigateHeaders = array("SOAPAction: xxx");
curl_setopt($ch, CURLOPT_POSTFIELDS, $investigateXML);
curl_setopt($ch, CURLOPT_HTTPHEADER, $investigateHeaders);
$investigateResponse = curl_exec($ch);
echo "</br>";
echo $investigateResponse;
curl_close($ch);
Problem is, system this code should be deployed on does not allow to store cookies and code simply does not work.
So, how can I amend this code to use PHP variables instead of temp file to maintain HTTP session?
Thank you
You can use php://memory stream to save session information in memory instead of a filesystem, if you cannot use the filesystem.
Alternatively, you can parse a session id from response Set-Cookie header on first request, save it to a variable and send it in a Cookie header or by means of CURLOPT_COOKIE on every subsequent request.
I am working with an API that provides a token for me to use to connect. It gives these instructions.
This token is then sent in all subsequent calls to the server in header variable Auth Digest.
I am not sure what this means. I've tried several approaches and read several stack overflow questions. Here's what I've tried. See code comments included for details.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,true);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
// I have tried setting both of these
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
// In combination of the above separately, I have tried each of these individually
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $token);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Auth Digest: ' . $token));
curl_setopt($ch, CURLOPT_POST, true);
$post_data = array('Auth Digest' => $token);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));
curl_setopt($ch, CURLOPT_USERPWD, $token);
// Then I execute and close, either giving me a failed response and a response that says token is not valid
$response = curl_exec($ch);
$header_sent = curl_getinfo($ch, CURLINFO_HEADER_OUT);
if (!$response) {
echo $action . ' curl request failed';
return false;
}
curl_close($ch);
$response_json = json_decode($response);
var_dump($response_json);
Here are some related stackoverflow questions that I've tried to apply to my problem without success.
Curl request with digest auth in PHP for download Bitbucket private repository
Client part of the Digest Authentication using PHP POST to Web Service
How do I make a request using HTTP basic authentication with PHP curl?
I need to know what the raw http header that they're likely expecting or how I can use php curl to generate the header they're likely expecting.
An digest authorization header typically looks like:
Authorization: Digest _data_here_
So in your case, try:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
//... existing options here
$headers = array(
'Authorization: Digest ' . $token,
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
If you use CURLOPT_HTTPHEADER, that just specifies additional headers to send and doesn't require you to add all the headers there.
If the other headers you're sending all have explicit options, use those and just pass the one authorization header to CURLOPT_HTTPHEADER.
i am using with curl libary to scrape web site.
when i am using proxy nothing now shown.
i took the ip proxy from hide my ass list.
here is my code. someone can tell me what is wrong ?
$url = 'http://www.sherut24.co.il';
$proxy = '189.218.145.44:10000';
$ch = curl_init(); // Initialise a cURL handle
// Setting proxy option for cURL
// Set any other cURL options that are required
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_URL, $url);
$results = curl_exec($ch); // Execute a cURL request
curl_close($ch); // Closing the cURL handle
echo $results;