PHP Curl wrong headers? - php

I'm sending content from a PHP Curl script to an API.
I'm using this is to do a POST do my script while passing json headers
$query = new stdClass;
$query->test = 'test';
$query = json_encode($query);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://localhost');
curl_setopt($ch, CURLOPT_HEADER, ['Content-Type: application/json', 'Content-Length: '.strlen($query)]);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_VERBOSE, true);
$res = curl_exec($ch);
curl_close($ch);
But when I trace what the content type of the request in on the API side, I get
var_dump($_SERVER['CONTENT_TYPE']);
//application/x-www-form-urlencoded
Shouldn't I get this instead?
application/json

You should use CURLOPT_HTTPHEADER instead of CURLOPT_HEADER
CURLOPT_HEADER can be true/false and define whether include header to response or not
FYI:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
These lines are redundant as you are not using https

Related

Curl returns no data (NULL) PHP

Very simple code in PHP, that looks like this:
curl_setopt($ch, CURLOPT_URL, "http://[heres url]");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('keycode: [heres keycode]'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
var_dump(json_decode($output, true));
// echo $output; doesn't work as well
[heres url], [heres keycode] - url and keycode are correct
If I run the same script in a app (for example I am using a chrome app named Advanced REST Client) - everything works.
In my PHP script the result is either empty or NULL.
What could be the problem? Thanks.
$url='https://randomuser.me/api/';
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION , 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER,array('keycode: [here value]'));
curl_setopt($ch, CURLOPT_URL,$url);
$html = curl_exec($ch);
var_dump(json_decode($html, true));

Get some data from external website via cURL

I want to pull some html pice of code with some data from an external website with cURL, but a recived a null response. If I type the url in browser i get the data that i want but i need to do that from my app via cURL or file_get_contents();
This is my code :
$_awb = '888000074599';
$url = 'http://89.45.196.45';
$post_fields = ':8080/?id=8IM*8J*9K&NT='.$_awb;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
$result = curl_exec($ch);
curl_close($ch);
You should not be using CURLOPT_POSTFIELDS. You are trying to do a GET request with a query string, but postfields is intended for making POST requests.
So setting CURLOPT_URL to the proper (absolute) URL should work.
$url = "http://89.45.196.45:8080/?id=8IM*8J*9K&NT="
$nt = "888000074599";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url . $nt);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close ($ch);

php- curl isn't working for POST requests

I want to send a POST request using curl but don't know why it isn't working. The HTTP request is as:
Type: POST
URL: https://abc.com/server/386594/actions/?do=open
HEADERS: Authorization: Basic 1234abc
When I am make request using curl (CLI) then it works fine but in php I do not get any response. My code is:
$headers = array("Authorization: Basic 1234abc");
$ch1 = curl_init();
curl_setopt($ch1, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch1, CURLOPT_URL, "https://abc.com/server/386594/actions/");
curl_setopt($ch1, CURLOPT_POST, 1);
curl_setopt($ch1, CUTLOPT_POSTFIELDS, "do=open");
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);
$vnc = curl_exec($ch1);
echo $vnc;
echo curl_error($ch1);
Where am I wrong? :)
Please put the code as below....
url is not secure url, so that remove the HTTPS and write simple HTTP.
$headers = array("Authorization:Basic 1234abc");
//remove the space
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$host_url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST,true);
//remove 1 and put 'true' keyword
curl_setopt($ch, CURLOPT_POSTFIELDS,"do=open");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
$curl_response = curl_exec($ch);

No Response getting from Curl Request to https server

I am creating a PHP curl request to server and expecting some string in response but no response generating , What may be the reason for that?
Below is the Curl code for the same:
$fields = array("username"=> 'xxx',"domain"=> 'xxx',"password" => 'xxx');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://host:8090/add-user");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $usr.':'.$pass);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
if(!$result=curl_exec($ch)){
trigger_error(curl_error($ch));
}
curl_close($ch);
var_dump($result);
Below is equivalent command line request for the same:
curl --data 'username=xxx&domain=xxx&password=xxx' https://host:8090/add-user -k -v -u 'usr:pass'
What i am doing Wrong Here?
You should first try to use curl_setopt($ch, CURLOPT_VERBOSE, $file_handler); where $file_handler = fopen('somefile.log', 'a'). Thus you will see what exactly cURL does, and where it fails.

cURL XML Post issue

Im posting some XML to a webservice, the code I have written works in all other cases just not in this instance.
This is my code;
$ch = curl_init();
curl_setopt($ch, CURLOPT_POSTFIELDS, $string);
curl_setopt($ch, CURLOPT_ENCODING,'gzip');
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 45);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: text/xml"));
$result = curl_exec($ch);
curl_close($ch);
return $result;
It keeps returning a 500 internal server error which is the same as if you go direct to the url so it's like the POST data isn't being sent. But I haven't check the content length and it seems to be..
Any Ideas?
try just this
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_string); // NOTE used $xml_string to indicate its just a text string of XML format
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_NOPROGRESS, 0);
$result = curl_exec($ch);
curl_close($ch);

Categories