CURL post sending PHP - php

Please look the codes as below :
$url='https://www.test.com/test.php';
$post='?field1=1&field2=2&filed3'; // no need array text as is
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
curl_exec ($ch);
curl_close ($ch)
Should be simple code, I used as reference http://curl.haxx.se/libcurl/php/examples/simplepost.html
I modified the code by replacing to variables.
I need to send data to remote server which belong to third party. Other side's server have data base. When I copy manually the www.test.com/test.php?field1=1&field2=2&filed3 into web browser's then the data saved into data base at other server and having respond {"Code":15,"Msg":null"} on browser screen, that's mean data sent properly. When trying to send by PHP script, the data not save in remote data base also not getting respond message.

If the request works when you enter it into a web browser, chances are it is a GET rather than a POST request. A simple example of doing that would be as follows:
$url= 'http://www.test.com/test.php?field1=1&field2=2&filed3';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
if (curl_errno($ch)) {
echo 'Error: ' . curl_error($ch);
}
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
var_dump($http_status);
var_dump($result);
I've also set the CURLOPT_RETURNTRANSFER option so you can capture the response in $result.

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.

command-line cURL to PHP doesn't work?

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?

Accessing Actiontec modem screens via PHP

I have an Actiontec V1000H router. I want to access its "WAN Ethernet Status" page using a script (which will extract the sent and received packet counts for plotting). From a browser, this URL works fine:
http://192.168.1.1/modemstatus_wanethstatus.html
But, when I use that URL in my script, I nearly always get the main screen. (It works on rare occasions.) Here's my script:
$wanStatusUrl = "http://192.168.1.1/modemstatus_wanethstatus.html";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $wanStatusUrl);
curl_setopt($ch, CURLOPT_USERPWD, 'admin:myPassword');
$output = curl_exec($ch);
curl_close($ch);
I need help accessing the modemstatus_wanethstatus.html page. I believe the issue is due to some idiocycracy of the modem.
Use this so that curl return you the html source as response into your $output:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
The main screen has a "login" button, and adding the equivalent of that prior to accessing the WAN Status screen made it work. So, for the record:
// login
$loginUrl = 'http://192.168.1.1/login.cgi?inputUserName=admin&inputPassword=myPassword&nothankyou=1';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $loginUrl);
curl_exec($ch);
curl_close($ch);
// get status page
$wanStatusUrl = "http://192.168.1.1/modemstatus_wanethstatus.html";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $wanStatusUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // so curl_exec returns the response
$responseText = curl_exec($ch);
curl_close($ch);
// print $responseText; // contains wanEthStatus_ReceivedPackets and wanEthStatus_SendPackets
// get the two packet counts ... wanEthStatus_ReceivedPackets and wanEthStatus_SendPackets
preg_match( "/wanEthStatus_ReceivedPackets.*?\'(\d+)\';.*?\'(\d+)\';.*?wanEthStatus_TimeSpan/s", $responseText, $matches );
print_r( $matches );
"Man Always Wins in the End."

server doesn't respond on POST Request with CURL + PHP

I have a simple PHP Script, that uses CURL to send a HTTP Post Request to a remote server.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://91.250.77.10/test.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('a' => 'b'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$contents = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
if ($error)
echo $error;
else
echo $contents;
This results in an error: "no response from server". The request can't be found in the access log of the remote server, too!
What's more, if I send the postfields as a querystring, i.e.:
curl_setopt($ch, CURLOPT_POSTFIELDS, '&a=b');
then everything is just fine.
It seems like something is wrong with the Apache or PHP configuration on the remote server. Any hints?
Edit:
As for now, it looks like the Server doesn't accept (or correctly handle) requests with Content-Type: multipart/form-data
(CURL uses that type when setting an array as the postfields, but not when setting a string.)
Since I need to send a file with the request, i have to use the multipart/form-data. So how do I get the server to correctly handle this?
If you pass an array to CURLOPT_POSTFIELDS then the form is submitted as multipart/form-data There could be a possibility your remote end is not accepting this encoding type.
Best way to do POST request is to format postFields like this.
VARIABLE = VALUE separated by &.
TIP:
$postFields = 'var1=cons1&var2=cons2&var3=cons3&var4=cons4&var5=cons5';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://91.250.77.10/test.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields;
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$contents = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
if ($error)
echo $error;
else
echo $contents;
Would be Pleased to do any more help.
Check if the HTTP header contains "Expect:100-continue" field. Some Servers do not deal with this field and just wait.
You can try:
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect: '));
For more information, pls see:
curl http 100 continue and multipartform-data post

PHP - How to fire HTTP get request on a secured URL (HTTPS)?

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);

Categories