How to maintain cURL HTTP session without storing a cookie? - php

This is PHP cURL code I'm using to maintain HTTP session with cookies
if(!function_exists("\curl_init")){
\load_curl();
echo "curl loaded";
} else {
echo "curl already exists ";
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, ''); //could be empty, but cause problems on some hosts
curl_setopt($ch, CURLOPT_COOKIEFILE, ''); //could be empty, but cause problems
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, $url1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POSTFIELDS, $startInterview);
curl_setopt($ch, CURLOPT_HTTPHEADER, $startInterviewHeaders);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
//curl_setopt($ch, CURLOPT_HEADER, 1);
$startInterviewresponse = curl_exec($ch);
//echo $response;
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curl_errno= curl_errno($ch);
echo "</br> HTTP status: " . $http_status . "</br> cURL error: " .$curl_errno . "</br>";
//curl_close($ch); // close cURL
echo $startInterviewresponse;
$investigateHeaders = array("SOAPAction: xxx");
curl_setopt($ch, CURLOPT_POSTFIELDS, $investigateXML);
curl_setopt($ch, CURLOPT_HTTPHEADER, $investigateHeaders);
$investigateResponse = curl_exec($ch);
echo "</br>";
echo $investigateResponse;
curl_close($ch);
Problem is, system this code should be deployed on does not allow to store cookies and code simply does not work.
So, how can I amend this code to use PHP variables instead of temp file to maintain HTTP session?
Thank you

You can use php://memory stream to save session information in memory instead of a filesystem, if you cannot use the filesystem.
Alternatively, you can parse a session id from response Set-Cookie header on first request, save it to a variable and send it in a Cookie header or by means of CURLOPT_COOKIE on every subsequent request.

Related

PHP - CURL error when trying to connect with proxy

I am trying to connect a website with CURL using a proxy in PHP. But I get "Received HTTP code 400 from proxy after CONNECT" error.
This is the code that I use to connect:
<?PHP
$ch = curl_init("https://ipinfo.io/json");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_PROXYTYPE, "HTTP");
curl_setopt($ch, CURLOPT_PROXY, "207.154.231.213:8080");
$output = curl_exec($ch);
if(curl_errno($ch))
echo curl_error($ch);
echo $output;
?>
I don't know what the problem is. The proxy should work perfectly because I check it.
use nex code
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, "https://ipinfo.io/json");
$result=curl_exec($ch);
curl_close($ch);
echo json_decode($result, true);
I recommend http://docs.guzzlephp.org/ library for apis for better practices
Based on the documentation curl_setopt you should either remove your CURLOPT_PROXYTYPE setting (to use the default CURLPROXY_HTTP), or you need to change your invalid setting
curl_setopt($ch, CURLOPT_PROXYTYPE, "HTTP");
to
curl_setopt($ch, CURLOPT_PROXYTYPE, "CURLPROXY_HTTP");
Note that the type HTTP does not exist.

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

cURL returns an empty string

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.

consume c# rest web service in php

I am trying to connect to a c# rest web service by php, but when I execute the below code this error happened:
"Message":"The requested resource does not support http method 'GET'".
Could anybody help me about this error?
<?php
$url="https://example/login";
$username='user';
$password='pass';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD,$username':'$password);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$out = curl_exec($ch);
print "error:" . curl_error($ch) . "<br />";
echo $out;
curl_close($ch);
?>
By default, curl is executing the request via HTTP GET. The web service has to be accessed via POST.
Have a look at here for details on how to create a POST request via curl.

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