Timeout caused potentially by curl_setopt | PHP - php

Essentially the following script is working fine when in local host but fails to load (Timeout) anytime loaded from a web server, is their something incorrectly written? I know it's not the end point api url that's the issue, since it works on local. Please see below, wondering if it's caused by curl_setopt:
$id = "dhl";
$number = "039103913";
// Parameters setting
$key = 'key';
$secret = 'scret';
$param = array (
'id' => $id,
'number' => $number,
'phone' => '',
'ship_from' => '',
'ship_to' => '',
'area_show' => 1,
'order' => 'desc'
);
// Request Json
$json = json_encode($param, JSON_UNESCAPED_UNICODE);
$signature = strtoupper(md5($json.$key.$secret));
$url = 'https://www.kd100.com/api/v1/tracking/realtime';
$headers = array (
'Content-Type:application/json',
'API-Key:'.$key,
'signature:'.$signature,
'Content-Length:'.strlen($json)
);
// Send post request
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$result = curl_exec($ch);
$data = json_decode($result, true);
echo json_encode($data);

Related

error_code 129 in VK api appWidgets.saveAppImage

Good day! I'm trying to load an image using the method appWidgets.saveAppImage. In the beginning I receive URL the server for loading -> getAppImageUploadServer, there all ok!
I receive a hash and an image, send them a POST request and get an error. Here is my code:
$token = "Service_access_key";
$tmp_image = file_get_contents('https://www.ejin.ru/wp-content/uploads/2017/12/667108931864_667108931864-150x150.jpg');
file_put_contents(dirname(__FILE__).'/tmp.jpg',$tmp_image);
$img_path = dirname(__FILE__).'/tmp.jpg';
$post_data = array("image" => "#".$img_path);
$upload_url = file_get_contents("https://api.vk.com/method/appWidgets.getAppImageUploadServer?v=5.85&image_type=50x50&access_token=".$token);
$url = json_decode($upload_url)->response->upload_url;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$result = json_decode(curl_exec($ch),true);
$safe = file_get_contents("https://api.vk.com/method/appWidgets.saveAppImage?v=5.85&hash=".$result['hash']."&image=".$result['image']."&access_token=".$token);
echo $safe;
echo:
"error_code":129,"error_msg":"Invalid photo: file not found, from upl_850128?act=app_widget_image"
what's my mistake?
<?
$request_params = array(
'image_type' => '510x128',
'access_token' => 'xxx',
'v' => '5.92'
);
$t = json_decode(file_get_contents('https://api.vk.com/method/appWidgets.getAppImageUploadServer?'. http_build_query($request_params)));
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:multipart/form-data"));
curl_setopt($ch, CURLOPT_URL, $t->response->upload_url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, array("image" => new CURLFile(dirname(__FILE__).'\img.jpg')));
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
$result = json_decode(curl_exec($ch));
curl_close($ch);
$request_params = array(
'hash' => $result->hash,
'image' => $result->image,
'access_token' => 'xxx',
'v' => '5.92'
);
print_r(file_get_contents('https://api.vk.com/method/appWidgets.saveAppImage?'. http_build_query($request_params)));
?>

Error code 410 when polling Skyscanner flight live prices

I am polling live prices from the Skyscanner API. Although I receive the session_key and although I am immediately polling the results I am getting a 410 (Gone) response header with an empty body. It used to work fine from my localhost environment but not on my live server anymore.
Has anybody experienced this before and can maybe give me a hint what the issue could be?
$url_api = "http://partners.api.skyscanner.net/apiservices/pricing/v1.0/";
$api_key = "XXX"; // Not shown here
$data = array('apiKey' => $api_key, 'country' => 'DE', 'currency' => 'EUR',
'locale' => 'de-DE', 'originplace' => 'HAM', 'destinationplace' => 'AMS', 'cabinclass' => 'economy', 'outbounddate' => '2017-01-27',
'inbounddate' => '2017-01-30' , 'locationschema' => 'Iata', 'groupPricing' => true);
$httpdata = http_build_query($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url_api);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $httpdata);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded', 'Accept: application/json'));
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
$response = curl_exec($ch);
curl_close($ch);
$headers = get_headers_from_curl_response($response);
$url = $headers['Location']."?apiKey=".$api_key."&stops=0";
echo $url;
return;

cURL post not working PHP

I'm having trouble using cURL for a specific page.
A live code working: http://svgen.com/jupiter.php
Here is my code:
$url = 'https://uspdigital.usp.br/jupiterweb/autenticar';
$data = array('codpes' => 'someLogin', 'senusu' => 'somePass', 'Submit' => '1');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_COOKIEJAR, "FileHere");
curl_setopt($ch, CURLOPT_COOKIEFILE, "FileHere");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_HTTP);
curl_exec($ch);
curl_close($ch);
Although I had used the same url and post data, file_get_contents worked:
$options = array('http' => array('method' => 'POST','content' => http_build_query($data)));
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
var_dump($result);
Someone could help me?
Thanks.
Most probably it is the SSL verification problem.
Add
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
Also if you use CURLOPT_PROTOCOLS option, it should be HTTPS since you are posting to a secure url
curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS); // you currently have http
$data = array('codpes' => 'someLogin', 'senusu' => 'somePass', 'Submit' => '1');
should have been
$data = http_build_query(array('codpes' => 'someLogin', 'senusu' => 'somePass', 'Submit' => '1'));
It automatically url encodes your query string as well and is safer than manual methods..
make your post data as:
$data = array('codpes' => 'someLogin', 'senusu' => 'somePass', 'Submit' => '1');
$postData = "";
foreach( $data as $key => $val ) {
$postData .=$key."=".$val."&";
}
$postData = rtrim($postData, "&");
and change:
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
to
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
Try these:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_CAINFO, '/etc/pki/tls/cert.pem'); // path is valid for RHEL/CentOS
This makes sure the resource you're "curling" has a valid SSL certificate. It is not recommended to set "CURLOPT_SSL_VERIFYPEER" to false (0).
You're on a secure connection, why are you using :
curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_HTTP);
Use instead :
curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);

Simple HTML Dom and Curl

Hi is curl the best method for using POST with cookie and also navigated to another page for scrapping? I'm using the coding below and I can't get it to work.
include('simple_html_dom.php');
$data = array(
'__EVENTTARGET' => '',
'__EVENTARGUMENT' => '',
'__VIEWSTATE' => '%2FwEPDwUKLTcyODA2ODEwMGRk',
'Myname' => 'justdev12345',
'Mypassword' => '12345',
'idLogin' => 'Login',
'tmplang2' => '6',
'fm' => '',
'jc' => '',
'LoginRef' => ''
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://secure.site.com/mainframe.aspx");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_COOKIEJAR, "sdc_cookies.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, "sdc_cookies.txt");
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
$output = new simple_html_dom();
$output = file_get_html('http://profiles.site.com/profile_900.aspx?AccountID=ShopCartUpdate');
print $output;
Your code is fine up until the end where you throw away the curl response and load the url with simple-html-dom. If you want to use the curl response it should look like this:
$html = str_get_html($output);
$html->find('title');
Otherwise you should probably remove all the curl code.

multiple facebook posting using curl

i need to post multiple message to facebook wall from the mysql database. first i fetch the data from mysql and put it in while loop
while($row=mysql_fetch_array($result))
{
$des=$row[1];
$purpose=$row[3];
$price_sale=$row[4];
$price_rent=$row[5];
$img="example.com/images".mysql_result($result,0,2);
$attachment = array(
'access_token' => "$token",
'message' => $des,
'picture' => $img,
'link' => "example.com"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'https://graph.facebook.com/xxxxxxxxxxx/feed');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //to suppress the curl output
$result = curl_exec($ch);
curl_close ($ch);
echo $result;
}
the $result contain 3 records. But only post the first row. Plz give a solution for this
Try changing the name of the variable that accepts the curl output.You are using the same variable above.
while($row=mysql_fetch_array($result))
{
$des=$row[1];
$purpose=$row[3];
$price_sale=$row[4];
$price_rent=$row[5];
$img="example.com/images".mysql_result($result,0,2);
$attachment = array(
'access_token' => "$token",
'message' => $des,
'picture' => $img,
'link' => "example.com"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'https://graph.facebook.com/xxxxxxxxxxx/feed');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //to suppress the curl output
$curlresult = curl_exec($ch);
curl_close ($ch);
echo $curlresult;
}

Categories