cURL returns an empty string - php

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.

Related

Getting JSON data from API with PHP and curl

There is an API page and I want to get the whole API data with the help of PHP+curl
For example the url is https://example.com/api which contains the following:
{"data": [{"information":{"username": "john","id": 15}]}
I want to have on my php result page: {"information":{"username": "john","id": 15}
using GET method.
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://example.com/api');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
print_r($result);
curl_close($ch);
?>
and I get white page.Where is the error?

How to convert curl "-b" tag into php curl?

I try to convert this curl command (proxmox api) into php curl : curl -k -b "PVEAuthCookie=PVE:root#pam:4EEC61E2::rsKoApxDTLYPn6H3NNT6iP2mv..." https://10.0.0.1:8006/api2/json/
I used the site https://incarnate.github.io/curl-to-php/ to convert it but it doesn't support the "-b" tag.
After some search, I tried this code :
<?php
$apiurlvmName = "https://10.0.0.1:8006/api2/json/";
$proxmoxid = "username=root#pam&password=mypass";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiurlvmName);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $proxmoxid);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_COOKIE, "PVEAuthCookie=PVE:root#pam:4EEC61E2::rsKoApxDTLYPn6H3NNT6iP2mv...");
$headers = array();
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
else {
echo $result;
}
curl_close($ch);
?>
But it doesn't works. The page takes few seconds to load then I get a blank page without error displayed. As if "curl_setopt($ch, CURLOPT_COOKIE, "PVEAuthCookie=PVE:root#pam:4EEC61E2::rsKoApxDTLYPn6H3NNT6iP2mv...");" were not enough.
Did I missed something ? How can I convert the curl "-b" tag correctly into php ? Thank you.
It has already been answered hundreds of times. But just in case, let's copy it here again for those who can't find it.
-b means "COOKIE".
# sending manually set cookie
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Cookie: PVEAuthCookie=PVE:root#pam:4EEC61E2::rsKoApxDTLYPn6H3NNT6iP2mv..."));
# sending cookies from file
curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile);

Connecting to VCenter with PHP using REST API authentication error

I followed the instructions in the official vsphere site to get info from the server and from the answer of another user here .
From what I have understood, firstly I have to get the session id (cis id), but I got "null" as a result.
I tried multiple codes but the result is the same:
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$headers = array(
'Content-Type: application/json',
'Accept: application/json',
'vmware-use-header-authn: test'
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, "https://vcsa/rest/com/vmware/cis/session");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, 'administrator#vs.dom:userpassword');
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC );
$out = json_decode(curl_exec($ch));
// var_dump($out);
if ($out === false) {
echo 'Curl Error: ' . curl_error($ch);
exit;
}
$sid = $out;
dd($out);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("vmware-api-session-id:$sid"));
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_URL, "https://vcsa/rest/vcenter/vm");
$output = curl_exec($ch);
$vms = json_decode($output);
var_dump($vms);
curl_close($ch);
The result is null.
this is a script word 100% using PHP
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, "https://{ip}/rest/com/vmware/cis/session");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, 'username' . ":" . 'password');
$out = json_decode(curl_exec($ch));
// var_dump($out);
if ($out === false) {
echo 'Curl Error: ' . curl_error($ch);
exit;
}
$sid = $out->value;
curl_setopt($ch, CURLOPT_HTTPHEADER, array("vmware-api-session-id:$sid"));
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_URL, "https://{ip}/rest/vcenter/vm");
$output = curl_exec($ch);
$vms = json_decode($output);
var_dump($vms);
curl_close($ch);
?>
when you debug your code, the $out variable return null?
If yes, can you check if your curl returns 56 - OpenSSL SSL_read: Success error in $out = json_decode(curl_exec($ch));?
This error occoured with my code, and I see wich cURL in 7.67 version cause this trouble.
My solution was to downgrade cURL version to 7.65.
It works for me!
I had the same problem, and eventually understood that it is caused by this header:
curl_setopt($ch, CURLOPT_POST, true);
And replacing it with this one solves the issue:
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');

PHP - file get contents using CURL [duplicate]

This question already has answers here:
PHP CURL & HTTPS
(4 answers)
Closed 4 years ago.
i have a problem with curl, i trying to get content using CURL but return just null, i dont know what i miss, please tell me, here is my code :
$url = "https://shopee.co.id/api/v1/search_items/?by=pop&order=desc&keyword=yi 067&newest=0&limit=50&page_type=shop&match_id=16775174";
$html = file_get_contents_curl($url);
var_dump($html);
function file_get_contents_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_ENCODING, 0);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST , "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json'));
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
With curl, it's often a good idea to use curl_getinfo to obtain additional information about your curl connection before closing it. In your case, the NULL/FALSE/empty result could be due to a number of reasons and inspecting the curl info might help you find more detail. Here's a modified version of your function that uses this function to obtain additional information. You might consider writing print_r($info, TRUE) to a log file or something. It might be empty because the server response is empty. It might be false because the url cannot be reached from your server due to firewall issues. It might be returning an http_code that is 404 NOT FOUND or 5XX.
function file_get_contents_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_ENCODING, 0);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST , "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$data = curl_exec($ch);
$info = curl_getinfo($ch);
if(curl_errno($ch)) {
throw new Exception('Curl error: ' . curl_error($ch));
}
curl_close($ch);
if ($data === FALSE) {
throw new Exception("curl_exec returned FALSE. Info follows:\n" . print_r($info, TRUE));
}
return $data;
}
EDIT: I added curl_errno checking too.

Fiddler to capture external webserver in PHP curl

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

Categories