I am trying to post the an xml string using curl.
Here is the code
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, 120);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 120);
curl_setopt($curl, CURLOPT_ENCODING, 'utf-8');
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'SOAPAction:""',
'Content-Type: text/xml; charset=utf-8',
));
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($curl, CURLOPT_SSLCERT, '.............');
curl_setopt($curl, CURLOPT_SSLCERTTYPE, 'PEM');
curl_setopt($curl, CURLOPT_SSLKEY, '.............');
curl_setopt($curl, CURLOPT_SSLKEYPASSWD, '');
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curlData);
curl_setopt($curl, CURLOPT_HEADERFUNCTION, 'read_header'); // get header
$result = curl_exec($curl);
This is the result I keep on getting
Resource id #4
I'm new to curl but I'm trying to study it. Is that the right code in my curl php?
I expect this sample response from curl:
{"status_code":"....","message":"..........."}
Related
I'm trying to realize a simple authorization using login and password. I get a cookie but not sure if authorized or not (how to check?), also I am trying to redirect after authorization but as a result, I get Error 404 after my code tried to redirect (followlocation, true).
Here's my php code
Trying just to redirect but 404 occurred.
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
$result = curl_exec($curl);
echo $result;
curl_close($curl);
And here's trying to authorize, not sure if it works (but I get cookie and in header I get 200 OK)
$loginData = [
urlencode('j_username') => 'someName',
urlencode('j_password') => 'somePassword'
];
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_VERBOSE, 2);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($loginData));
curl_setopt($curl, CURLOPT_COOKIEJAR, $_SERVER['DOCUMENT_ROOT'].'/authCookie.txt');
curl_setopt($curl, CURLOPT_COOKIEFILE, $_SERVER['DOCUMENT_ROOT'].'/authCookie.txt');
$result = curl_exec($curl);
echo $result;
curl_close($curl);
Also, I use urlencode but not sure if I use it in the right way (I have it in curl like $curl --location --request POST 'http://someFullName' \ --data-urlencode 'j_username=someUsername' \ --data-urlencode 'j_password=somePassword')
for auth you can use http basic auth it very simple in use for client and server, client code in this case will be look like:
<?php
const COOKIE_FILE = '/tmp/authCookie';
$host = 'localhost';
$username = 'login';
$password = 'pass';
$ch = curl_init($host);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_USERPWD, sprintf('%s:%s', $username, $password));
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIE_FILE);
curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIE_FILE);
$return = curl_exec($ch);
curl_close($ch);
about your 4xx error, i think you have problems with server, for test redirections you can do something like:
<?php
//stable route with redirect
$host = 'https://bitly.is/EnterpriseButton';
$userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:76.0) Gecko/20100101 Firefox/76.0';
$curl = curl_init($host);
curl_setopt($curl, CURLOPT_USERAGENT, $userAgent);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, $host);
//with enabled followlocation
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_HEADER, true);
$result = curl_exec($curl);
curl_close($curl);
var_dump($result);
$curl = curl_init($host);
curl_setopt($curl, CURLOPT_USERAGENT, $userAgent);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, $host);
//with disabled followlocation
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($curl, CURLOPT_HEADER, true);
$result = curl_exec($curl);
curl_close($curl);
var_dump($result);
and for check auth work you can auth through browser with cookies from curl
environment:
OS:macOS
PHP:7.1.30
curl:7.65.0
openssl:1.0.2r
I've been trying to call https api with php curl library,it return 403 http code on my machine but 200 on my workmate's Mac.
this is the output html image
my curl configure code:
$curl = curl_init();
curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 0);
curl_setopt($curl, CURLOPT_SSLVERSION, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64)');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
the configure code above works on my workmate's Mac.
But if I set CURLOPT_SSL_VERIFYHOST = 1 or 2, it works too.
Has anyone been in this situation?
I am trying to view states from a page that requires login (i have premium membership)
but when running the script it does not retrieve the logged in information - is my login curl script missing something?
$curl=curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, 'https://reg.racingpost.com/mpp/sign_in.sd');
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_POSTFIELDS, array('signinEmail'=>'...#....com', 'signinPassword1'=>'.....'));
curl_setopt($curl, CURLOPT_COOKIEJAR, dirname(__FILE__).'/cookie.txt');
curl_exec($curl);
curl_close($curl);
$curl=curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, 'http://www.racingpost.com/horses/result_home.sd?race_id=650156&r_date=2016-05-29');
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($curl, CURLOPT_COOKIEFILE, dirname(__FILE__).'/cookie.txt');
echo curl_exec($curl);
curl_close($curl);
The problem is probably with the keys of the variables:
curl_setopt($curl, CURLOPT_POSTFIELDS, array('signinEmail'=>'...#....com', 'signinPassword1'=>'.....'));
Should be:
curl_setopt($curl, CURLOPT_POSTFIELDS, array('signInEmail'=>'...#....com', 'signInPassword1'=>'.....'));
^ here ^ and here
If http://www.racingpost.com/horses/result_home.sd?race_id=650156&r_date=2016-05-29 requires login then don't close the curl and make another curl request with the same Curl init. Se below code
$curl=curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, 'https://edeveloper.mppglobal.com/interface/Mpp/eDeveloper/v8/eDeveloper.json.svc/UserAuthenticateByEmail');
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode(array('EmailAddress'=>'...#....com', 'UserPassword'=>'.....')));
curl_setopt($curl, CURLOPT_COOKIEJAR, dirname(__FILE__).'/cookie.txt');
curl_exec($curl);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, 'http://www.racingpost.com/horses/result_home.sd?race_id=650156&r_date=2016-05-29');
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($curl, CURLOPT_COOKIEFILE, dirname(__FILE__).'/cookie.txt');
echo curl_exec($curl);
curl_close($curl);
I hope this will help.
$post_user_values = array(
'username' => USERNAME,
'password' => PASSWORD
);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_user_values);
i'm trying to make a curl request to a https based url. But seems nothing returned back..
I tested with many proxy that worked for non https.
$att = "cookie.txt";
$ip = "121.14.138.56:81";
$curl = curl_init("https://www.att.com");
curl_setopt($curl, CURLOPT_PROXY, $ip);
curl_setopt($curl, CURLOPT_COOKIEJAR, $att);
curl_setopt($curl, CURLOPT_COOKIEFILE, $att);
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/27.0.1453.94 Safari/537.36");
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true );
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 50);
curl_setopt($curl, CURLOPT_TIMEOUT, 50);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
$reply = curl_exec($curl);
curl_close($curl);
echo $reply;
Seems this code is working. Note: only some proxy support https..
$proxy = "205.221.221.111:8080";
$curl = curl_init("https://www.att.com");
if (isset($proxy)) {curl_setopt($curl, CURLOPT_PROXY, $proxy);}
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_COOKIEJAR, $att);
curl_setopt($curl, CURLOPT_COOKIEFILE, $att);
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/27.0.1453.94 Safari/537.36");
curl_setopt($curl, CURLOPT_COOKIE, 'cookie.txt');
curl_setopt($curl, CURLOPT_AUTOREFERER, true);
curl_setopt($curl, CURLOPT_COOKIESESSION, true);
curl_setopt($curl, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($curl, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true );
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
$reply = curl_exec($curl);
curl_close($curl);
var_dump(curl_getinfo($curl));
echo $reply;
cURL gives me the error:
Operation timed out after 0 milliseconds with 0 out of 0 bytes received
In particular, the "0 milliseconds" part is suspicious...
My initialization code:
$curl = curl_init($requestUrl); // private URL not published
curl_setopt($curl, CURLOPT_FRESH_CONNECT, true);
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_HTTPHEADER,
array("Content-Type: application/xml", "Accept: application/xml"));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 20);
curl_setopt($curl, CURLOPT_FAILONERROR, false);
curl_setopt($curl, CURLOPT_HTTP200ALIASES, range(400, 599));
curl_setopt($curl, CURLOPT_SSLVERSION, 3);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
The timeout seems correctly set, what may be related it?
I had the same issue, when tried to connect via https. Problem was with ssl version.
This worked well for me:
$ch=curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSLVERSION, 3);