I'm trying to convert command-line cURL to PHP cURL, but it doesn't seem to work when I do it through PHP. I access the same URL and everything, but the API returns a general error with no data.
This works properly, it is accessed via POST with no post body, just parameters in the URL.
$curlRequest = 'curl -X POST -d "" --globoff \''.$baseURL.'?access_key='.$access_key.'&handles='.$handles.'&contextToken='.$contextToken.'&access_sig='.$access_sig.'\'';
exec($curlRequest, $result);
However, when I try to perform the same request with the cURL library in PHP, it doesn't work.
$url = $baseURL.'?access_key='.$access_key.'&handles='.$handles.'&contextToken='.$contextToken.'&access_sig='.$access_sig.'\'';
$ch = curl_init();
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, "");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
$server_output = curl_exec($ch);
curl_close ($ch);
$server_output just ends up being an empty error from the API instead of the same data that the first code block returns. Am I doing something incorrectly? Are the two requests not equivalent?
Related
I am using the following code to send a CURL request to send a SMS. But the SMS is being sent twice.
$message = urlencode($message);
$smsurl = "http://$url/sendmessage.php?user=matkaon&password=$password&mobile=$mobile&message=$message&sender=$sender&type=3";
$ch = curl_init($smsurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$sentsms = curl_exec($ch);
curl_close($ch);
I tried commenting some of the lines which solved the issue but gives an output as below:
What is the proper method to send a CURL request only once?
Try this:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $smsurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
Don't pass the URL as argument on the init function.
I don't know why the function is being called twice, but I never pass the URL as argument and always work great this way.
You'd usually use curl_init() with no parameters, then pass the URL into curl_exec.
Modified example 1 from curl_exec docs:
<?php
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $smsurl);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// grab URL and pass it to the browser
curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
?>
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.
I'm trying to do this command line curl command through PHP cURL, I'm getting the output perfectly in commandline but not the same when I try with PHP.
curl -d "text=terrible" http://text-processing.com/api/sentiment/
Tried to write a PHP cURL but I didn't get any output.
$data = "text=terrible";
$ch = curl_init('http://text-processing.com/api/sentiment/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
echo "<pre>";
print_r($response);
echo "</pre>";
Their API states they require the post to be form encoded data here sentiment api.
Excerpt
To analyze the sentiment of some text, do an HTTP POST to http://text-processing.com/api/sentiment/ with form encoded data containg the text you want to analyze. You’ll get back a JSON object response with 2 attributes:
To do this in PHP with curl, you must pass the header for Content-Type with the value application/x-www-form-urlencoded.
Demonstration
$data = "text=terrible";
$ch = curl_init('http://text-processing.com/api/sentiment/');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
echo "<pre>";
print_r($response);
echo "</pre>";
I think curl is not enable on your end. I tried your code and it is running properly.
Try to enable curl then try it.
Open your php.ini, situated in apache\bin.
Uncomment ;extension=php_curl.dll.
Restart Apache.
Hope this will help
This PHP code works using SoapClient.
$client = new SoapClient("http://www.roblox.com/Marketplace/EconomyServices.asmx?WSDL");
$response = $client->GetEstimatedTradeReturnForTickets(array("ticketsToTrade" => 1000));
echo $response->GetEstimatedTradeReturnForTicketsResult;
It echoes a number.
I plan on doing this on x10hosting (or any other free web host with 10 minute cron) and x10hosting doesn't support SoapClient.
So how would this be written without using Soap?
EDIT:
So I've also tried this and it didn't work.
<?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.roblox.com/Marketplace/EconomyServices.asmx/GetEstimatedTradeReturnForRobux");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,array("robuxToTrade" => 1000));
// 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);
echo $server_output
curl_close ($ch);
?>
For that specific call you can use CURL, see below. For more extensive SOAP requests you might want to look for a library to replace the missing SoapClient (see the comments under your question).
Example using CURL:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.roblox.com/Marketplace/EconomyServices.asmx/GetEstimatedTradeReturnForRobux");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "ticketsToTrade=1000");
...
Or just use other answers: PHP + curl, HTTP POST sample code?
I am trying to fire a HTTP GET request on a secured URL which asks for username and password. This is fine when I am using that from browser but I am not sure how to do that using PHP.
I have tried using the two methods:
1) Using Curl as suggested in here: Make a HTTPS request through PHP and get response
2) Using the file_get_contents as suggested in here: How to send a GET request from PHP?
But the first one didn't give me any response back. And the second one gave me the following error:
failed to open stream: HTTP request failed
And this is my code for the curl:
$url="https://xxxxx.com";
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
echo $response;
and for the file_get_contents:
$url="https://xxxx.com";
$response=file_get_contents($url);
echo $response;
The URL will return a XML response for a API I am testing. Can someone point me to the right direction?
Thanks!
If we focus on the requirement to send a username and password because I suspect that's your main problem, try this
$ch = curl_init();
$url="https://xxxxx.com";
// OR - check with your server's operator
$url="http://xxxxx.com";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
// or maybe
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
// - see http://stackoverflow.com/questions/4753648/problems-with-username-or-pass-with-colon-when-setting-curlopt-userpwd
// check the cURL documentation
$output = curl_exec($ch);
$info = curl_getinfo($ch);
// don't forget to check the content of $info, even a print_r($info) is better
// than nothing during debug
curl_close($ch);