This is part of my code that I use to login on a webpage (links changed):
$curl = curl_init ();
curl_setopt ( $curl, CURLOPT_URL, "loginpage");
curl_setopt ( $curl, CURLOPT_FOLLOWLOCATION, 1 );
curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $curl, CURLOPT_POST, 1 );
curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt ( $curl, CURLOPT_POSTFIELDS, "login=login&password=password");
curl_setopt ( $curl, CURLOPT_ENCODING, "" );
curl_setopt ( $curl, CURLOPT_COOKIEFILE, $_SERVER["DOCUMENT_ROOT"].'/web.cookies' );
curl_setopt ( $curl, CURLOPT_COOKIEJAR, $_SERVER["DOCUMENT_ROOT"].'/web.cookies' );
curl_setopt ( $curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)" );
curl_exec ( $curl );
curl_setopt ( $curl, CURLOPT_URL, "homepage");
$profilepage = curl_exec ( $curl );
The thing is, when I run
http://localhost/login.php
I can login and manage my web profile through my script.
The problem is, when I put all my scripts (including this one) on my external paid web server, ie.
http://myserver.com/login.php
I get a response from "loginpage" that "Cookies are not enabled".
What could possibly go wrong?
Related
I am trying to execute CURL via cron job, hosting is GoDaddy's shared linux hosting. When I execute the script from browser URL then it works, it is not working via cron job.
Following error:
curl: (35) error:14077410:SSL routines:SSL23_GET_SERVER_HELLO:sslv3 alert handshake failure
Please notice: GET request is working fine, it is only issue with POST request, I tried different solutions, but question is that why it is working for GET and not only POST. When I execute script via browser URL it works but it is only issue via cron job.
Following is my code for CURL
$postData = array(
"email" => "login",
"password" => "password",
);
$headers = array(
"Content-Type: application/json"
);
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,'https://reqres.in/api/login');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData));
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,2);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt ($ch, CURLOPT_HTTPPROXYTUNNEL, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$result = curl_exec ($ch);
if (empty($result)){
$return = "<hr><br>\n";
$return .= 'Errors: ' . curl_errno($ch) . ' ' . curl_error($ch) . '<br><br>';
$return .= "<hr><br>\n";
}
else{
$return = $result;
}
print $result;
curl_close ($ch);
Please suggest.
When dealing with curl requests to SSL enabled endpoints you'll have much greater success if the curl request includes options specifically geared towards ssl connections. The simple function below uses cacert.pem and other ssl specific options
function curl( $url=NULL, $options=NULL ){
$cacert='c:/wwwroot/cacert.pem';
$vbh = fopen('php://temp', 'w+');
$res=array(
'response' => NULL,
'info' => array( 'http_code' => 400 ),
'headers' => NULL,
'errors' => NULL
);
if( is_null( $url ) ) return (object)$res;
session_write_close();
/* Initialise curl request object */
$curl=curl_init();
if( parse_url( $url,PHP_URL_SCHEME )=='https' ){
curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, true );
curl_setopt( $curl, CURLOPT_SSL_VERIFYHOST, 2 );
curl_setopt( $curl, CURLOPT_CAINFO, $cacert );
}
/* Define standard options */
curl_setopt( $curl, CURLOPT_URL,trim( $url ) );
curl_setopt( $curl, CURLOPT_AUTOREFERER, true );
curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $curl, CURLOPT_FAILONERROR, true );
curl_setopt( $curl, CURLOPT_HEADER, false );
curl_setopt( $curl, CURLINFO_HEADER_OUT, false );
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $curl, CURLOPT_BINARYTRANSFER, true );
curl_setopt( $curl, CURLOPT_CONNECTTIMEOUT, 20 );
curl_setopt( $curl, CURLOPT_TIMEOUT, 60 );
curl_setopt( $curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36' );
curl_setopt( $curl, CURLOPT_MAXREDIRS, 10 );
curl_setopt( $curl, CURLOPT_ENCODING, '' );
curl_setopt( $curl, CURLOPT_VERBOSE, true );
curl_setopt( $curl, CURLOPT_NOPROGRESS, true );
curl_setopt( $curl, CURLOPT_STDERR, $vbh );
/* Assign runtime parameters as options */
if( isset( $options ) && is_array( $options ) ){
foreach( $options as $param => $value ) curl_setopt( $curl, $param, $value );
}
/* Execute the request and store responses */
$res=(object)array(
'response' => curl_exec( $curl ),
'info' => (object)curl_getinfo( $curl ),
'errors' => curl_error( $curl )
);
rewind( $vbh );
$res->verbose=stream_get_contents( $vbh );
fclose( $vbh );
curl_close( $curl );
return $res;
}
/* configure the request */
$data = array(
'email' => 'peter#klaven',
'password' => 'cityslicka'
);
/* the target endpoint */
$url='https://reqres.in/api/login';
$config=array(
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode( $data ),
CURLOPT_HTTPHEADER => array('Content-Type: application/json')
);
/* make the request */
$results=curl( $url, $config );
if( $results->info->http_code==200 ){
printf('<pre>%s</pre>',print_r( $results, true ));
}
Some info from the response object - notably verbose details ~ the full response includes the token ~ {"token":"QpwL5tke4Pnpja7X"} which suggests success!
* TCP_NODELAY set
* Connected to reqres.in (104.27.134.11) port 443 (#0)
* ALPN, offering http/1.1
* successfully set certificate verify locations:
CAfile: c:/wwwroot/cacert.pem
CApath: none
* SSL connection using TLSv1.2 / ECDHE-ECDSA-CHACHA20-POLY1305
* ALPN, server accepted to use http/1.1
* Server certificate:
* subject: OU=Domain Control Validated; OU=PositiveSSL Multi-Domain; CN=sni96286.cloudflaressl.com
* start date: Jan 23 00:00:00 2019 GMT
* expire date: Aug 1 23:59:59 2019 GMT
* subjectAltName: host "reqres.in" matched cert's "reqres.in"
* issuer: C=GB; ST=Greater Manchester; L=Salford; O=COMODO CA Limited; CN=COMODO ECC Domain Validation Secure Server CA 2
* SSL certificate verify ok.
Im using the following code:
$json_link = "https://graph.facebook.com/v2.12/me?fields=id,picture.width(300).height(280)&access_token=".$accessToken."";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $json_link);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$output = curl_exec($ch);
curl_close($ch);
$obj = json_decode($output);
$xx=$obj->picture->data->url;
$src=imagecreatefromstring(file_get_contents($xx));
$obj returns the following:
stdClass Object (
[id] => idxxxxx
[picture] => stdClass Object (
[data] => stdClass Object (
[height] => 480
[is_silhouette] =>
[url] => lookaside.facebook.com/platform/profilepic/
[width] => 415
)
)
)
But it is failing on:
$xx=$obj->picture->data->url;
$src=imagecreatefromstring(file_get_contents($xx));
I get:
So fails:
imagecreatefromstring(file_get_contents($xx));
It worked fine before March 27, but after graph api update its no longer working.
FB is looking for User-agent information.
$User_Agent = 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31';
$request_headers = array();
$request_headers[] = 'User-Agent: '. $User_Agent;
$request_headers[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$r = curl_exec($ch);
curl_close($ch);
It will solve your problem.
Only put the line: file_put_contents('file.jpg', fopen($obj->picture->data->url, 'r'));
I run into strange problem with GoogleMaps API.
Calling the following url in browser, everything works fine.
https://maps.googleapis.com/maps/api/geocode/json?components=country:DE|postal_code:81823&key=[mykey]
But using same url in PHP with curl I get a ZERO_RESULT?
Calling https://maps.googleapis.com/maps/api/geocode/json?components=country:DE|postal_code:90471 it works fine in PHP and Browser.
My code in PHP:
$timeout = 5;
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );
$decode = json_decode( curl_exec( $ch ) );
curl_close( $ch );
print_r($decode);
returns with the first url
stdClass Object
(
[results] => Array
(
)
[status] => ZERO_RESULTS
)
I have a small problem with php curl post.
I am trying to post some turkish characters to a forum but aren't posted how it should be.
This is how i save the text:
fwrite($fpp,"\xEF\xBB\xBF");
fwrite($fpp, $row['template']);
fclose($fpp);
and posting:
$this->curl = curl_init();
curl_setopt ( $this->curl, CURLOPT_URL, $this->vb_url . 'newthread.php?' . $url_vars );
curl_setopt ( $this->curl, CURLOPT_POST, true );
curl_setopt ( $this->curl, CURLOPT_POSTFIELDS, $post_fields );
curl_setopt ( $this->curl, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $this->curl, CURLOPT_CONNECTTIMEOUT,20);
curl_setopt ( $this->curl, CURLOPT_TIMEOUT,10);
curl_setopt ( $this->curl, CURLOPT_HEADER, true );
curl_setopt ( $this->curl, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt ( $this->curl, CURLOPT_COOKIE, $this->cookie_name );
curl_setopt ( $this->curl, CURLOPT_COOKIEJAR, $this->cookie_name );
curl_setopt ( $this->curl, CURLOPT_COOKIEFILE, $this->cookie_name );
curl_setopt ( $this->curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1');
$result = curl_exec ( $this->curl );
this is how it should be:
`Bölüm resimleri, dizi indirme ve altyazı linkine aşağıdan ulaşabilirsiniz.`
this is how it is posted:
`Bölüm resimleri, dizi indirme ve altyazı linkine aşağıdan ulaşabilirsiniz.`
Thanks
From http://php.net/manual/es/function.curl-setopt.php
Try adding:
curl_setopt($this->curl,CURLOPT_HTTPHEADER,array (
"Content-Type: application/x-www-form-urlencoded; charset=utf-8"
));
application/x-www-form-urlencoded suggested by #spencercw
Fixed with
$message = #iconv("UTF-8","Windows-1252//IGNORE",$message);
I'm trying to get windows live connections with php. I Get it with js with w.l. sdk but not with php.
This is my code:
$CLIENT_ID =variable_get('example_contact_grabber_hotmail_client_id',NULL);
$REDIRECT_URL = variable_get('example_contact_grabber_hotmail_url_callback',NULL);
//dpm($REDIRECT_URL);
$url = "https://oauth.live.com/authorize?client_id=$CLIENT_ID&scope=wl.signin&response_type=code&redirect_uri=$REDIRECT_URL";
$url = str_replace( "&", "&", urldecode(trim($url)) );
//dpm($url);
$ch = curl_init($url);
curl_setopt( $ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1" );
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE,0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, TRUE);
$salida = curl_exec($ch);
curl_close($ch);
$todo = split("\n",$salida);
//dpm($todo);
The request give me this message.
Array
(
[0] => <html><head><title>Object moved</title></head><body>
[1] => <h2>Object moved to here.</h2>
[2] => </body></html>
[3] =>
)
If I check $url in Firefox, it goes fine.
Try adding:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
//And change
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //true to false
Hope it helps