after curl post, how to check the postfield - to debug - php

i have a curl post somehing like this
curl_setopt($ch, CURLOPT_URL,$cpUrl);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $varpost); // post parameters
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // Return the output in string format
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_NOBODY, true);
$output = curl_exec ($ch); // Execute
how can i check after curl_close ($ch); what post field variables were sent? Please help

how can i check after curl_close ($ch); what post field variables were
sent?
Your script provides the variables sent with the CURLOPT_POSTFIELDS options, so if the cURL executed successfully, you already have those variables:
...
$output = curl_exec($ch); // Execute
//show the POST fields if they were sent successfully
if($output) print_r($varpost);
else echo "cURL failed: no POST fields sent";

You can use Fiddler to examine what data is actually beeing transfered between the client and the server

Related

php curl function not giving the required result

I need help with API Integration. When I echo the variable $url, I get the result, but I do not know why cURL is not working for me:
<?php
$token="43e6c623dda8f35df4bXXXfa5f0ec57d58e91154a ";
$format="json";
$waybill="974510010010";
$ref_nos="";//either this or waybill
$verbose="0";// meta info need to append in url
$url="https://test.delhivery.com/api/packages/json/?token=".$token."&format=".$format."&waybill=".$waybill."&ref_nos=".$ref_nos."&verbose=".$verbose;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPGET, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
echo curl_error($ch);
$return = curl_exec ($ch);
curl_close ($ch);
echo $return;
?>
Do you have access to the service you are connecting to?
It could be that the service requires the data as POST variables as opposed to the GET parameters you are sending.
Or perhaps some error handling on server that does not validate the waybill value you are sending. The ref_nos variable is also empty, which some applications could interpret as invalid. A tips would be to omit the ref_nos variable from the request string if its value is empty.

How to Post Action automatically in php?

Details are here:
First i will hit on this link eg. http://your.app/callback?code=abc123
then i will receive value from code variable from url then i want to redirect this url as a POST action in https://api.another.com/token
grant_type=authorization_code&
code=[CODE] url, bearing the value of code
Use Curl:
An example:
<?php
// if you get the code here from the url,
$code = $_GET['code'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://api.another.com/");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
"token_grant_type=authorization_code&code=" + $code);
// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
?>

PHP Script not able to make Http request

I have writtten this script in php to make a http request to http://ubaid.tk/sms/sms.aspx
Here's the script-
<?php
$connection_url = sprintf('http://ubaid.tk/sms/sms.aspx?uid=8149744569&pwd=passmsg=%s&phone=%s&provider=way2sms', $_REQUEST['message'], $_REQUEST['mobileno.']);
$ch = curl_init($connection_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // Return the result
curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/cookies.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, '/tmp/cookies.txt');
$data = curl_exec($ch); // Run the request
// Display the result
echo "<pre>";
print_r($data); /* result of SMS API call*/
echo '</pre>';
?>
And I need this script to send a http request as http://ubaid.tk/sms/sms.aspx/uid=814974&pwd=pass&msg=$_REQUEST['message']&phone=$_REQUEST['mobileno.']&provider=way2sms
The variables replaced and get back the response which the request gets and print it as it is. I have modified this script along with the code because I m still not able to get the correct output with it.
I need to convert it to POST request what more modifications do I need to do?
This should do the trick... but you should also add in some sanitization on your inputs to help protect against the possibility of injection (this is an entirely different discussion).
Sending Via GET
<?php
$connection_url = sprintf('http://example/sms/sms.aspx?uid=814974&pwd=pass&msg=%s&phone=%s&provider=way2sms', $_REQUEST['message'], $_REQUEST['mobileno.']);
$ch = curl_init($connection_url);
curl_setopt($ch, CURLOPT_HTTPGET, 1); // Make sure GET method it used
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // Return the result
curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/cookies.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, '/tmp/cookies.txt');
$data = curl_exec($ch); // Run the request
// Display the result
echo "<pre>";
print_r($data); /* result of SMS API call*/
echo '</pre>';
?>
Sending Via POST
<?php
// Setup Connection URL
$connection_url = sprintf('http://example/sms/sms.aspx');
// Setup Post Variables
$post_vars = sprintf('uid=814974&pwd=pass&msg=%s&phone=%s&provider=way2sms', $_REQUEST['message'], $_REQUEST['mobileno.']);
$ch = curl_init($connection_url);
curl_setopt($ch, CURLOPT_POST, 1); // Make sure POST method it used
curl_setopt($ch,CURLOPT_POSTFIELDS, $post_vars); // Attach post variables
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // Return the result
curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/cookies.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, '/tmp/cookies.txt');
$data = curl_exec($ch); // Run the request
// Display the result
echo "<pre>";
print_r($data); /* result of SMS API call*/
echo '</pre>';
?>
You were not escaping the string properly:
curl_setopt($ch,CURLOPT_POSTFIELDS,"uid=814974&pwd=pass&msg=".$_REQUEST['message']."&phone=".$_REQUEST['mobileno.']."&provider=way2sms");

PHP CURL post returning results but I need empty results

I'm doing curl post but the problem is that I need to return empty results so I can echo out another value for my ajax request results:
below is the code:
$ch = curl_init("http://www.rankreport.com.au/ajax/add_new_lead");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "lead_company_id=1&lead_business=1234&lead_first_name=asdf&lead_website=12314.com&lead_phone=1234&lead_email=test#test.com&lead_package=seo");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_exec($ch);
curl_close($ch);
echo 'error 3';
When I do this, I'm getting below results:
{"lead_date":1315637343,"lead_company_id":"1","lead_business":"1234","lead_first_name":"asdf","lead_last_name":"","lead_website":"12314.com","lead_phone":"1234","lead_email":"test#test.com","lead_package":"seo"} 3
How do set curl options so that it doesn't return any curl results?
Set CURLOPT_RETURNTRANSFER:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
Use CURLOPT_RETURNTRANSFER = true to have the returned content as a return value ($ch) instead of output.

PHP Curl Print Problem

I'm trying to print an external site's HTML using curl, but when I go to print the html I just get a bunch of bad characters:
D\ÓLþ¢GΖ´ï!ñ{HÑ,Jþ»H¹§L+÷53j?‰²î¡<‘*tÜe÷uÖbìê~Æô¬²c˜‹§ ~áäÆL#f?ⶊªþU™á˜ÉÉOæ{^¤ëaÀ Tê"1Û¨Dtî’œxˆk‘:#ŽD5î:'¶e\*³q‘׸`…±¾ôäó÷ð1j7þä‘åQ6®9bcxã„A2ã—-ÇøüåÉò÷2{ÂÐe桢ǙŒÄg©Az!Ø¡>±zךÂ+;f RZÛÝ€ížáÒžHa¬¢Æ'ë•ñ þ=Ð=ºtyšÖâå'ÇpžÄ¦ÆN½€5½roåðFe¹)ˆš`ØnhŠy(GÆÔ} Bu7H¥JzÐ iVê÷áÆ”øG>6HÿUµÞhµj YH-ÌaaEÚx±‰…Êâ£-ûeÎqCÆLÌå㘎‰†LÐÆM I]€a)Ï.$—ÅH••£ ŒŒ’ªÉ;cŽc(i´¥!I]‹€„³tFc^ë'€e±øÉ_øß
Here is my code:
$curl = curl_init();
curl_setopt ($curl, CURLOPT_URL, "http://www.thesite.com/");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec ($curl);
curl_close ($curl);
print $result;
Any help would be great,
Thanks!
If the response is GZIP'd, you may use this:
curl_setopt($curl, CURLOPT_ENCODING, 'gzip');
See: http://www.php.net/manual/en/function.curl-setopt.php
To see if it's gzip'd, check the headers (see CURLOPT_HEADER).

Categories