I have a very strange problem which I have been trying to figure out for 2 days, I have the below code to fetch information from a site for flight information, it works perfectly on my localhost and returns me "{thedata:what_I_Need}" but when I upload the script on my server it returns an "{invalid:true}" error from the site.
I tried it on 4 different droplets/vps, each in a different country IP with the same configuration (same curl-php-apache version and modules) as my local computer ( using ubuntu 14.4 with php5) but it still has the same issue. Here is the code which works perfectly on localhost:
$url = 'http://site.url.com';
$post = array(
'LanguageID' => 2,
'Trips' => array(array(
'Origin' => "JFK",
'Destination' => "TEX",
'Departure' => "2015-12-24T00:00:00",
)),
'Return' => "2015-12-28T00:00:00",
'Carriers' => null
'Adults' => 1,
'Children' => 0
);
$data_string = json_encode($post);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data_string);
curl_setopt($ch,CURLOPT_HEADER, 1);
curl_setopt($ch, 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");
curl_setopt($ch, CURLOPT_REFERER, "http://www.site.url.com");
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'X-Requested-With: XMLHttpRequest',
'Content-Length: ' . strlen($data_string))
);
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
$output=curl_exec($ch);
curl_close($ch);
I followed this article as well but I still have same issue on the remote site even if I replace CURLOPT_POST with
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
Related
I am wondering how do I make this code support arrays?
i'am trying to send parameters via php curl in a stock screener to have the result in this page:
https://finance.yahoo.com/screener/unsaved/f0171a68-053e-42f2-a941-d6ecdf2ba6d1?offset=25&count=25
parameters
here is my php code
<?php
$url = 'https://query1.finance.yahoo.com/v1/finance/screener?lang=en-US®ion=US&formatted=true&corsDomain=finance.yahoo.com';
// $url = 'https://finance.yahoo.com/screener/unsaved/f0171a68-053e-42f2-a941-d6ecdf2ba6d1';
$parameters =
[
'size' => 25,
'offset' => 50,
'sortField' => 'intradaymarketcap',
'sortType' => 'DESC',
'quoteType' => 'EQUITY',
'topOperator' => 'AND',
'query' => array(
'operator' => 'AND',
'operands'=> array(
'operator' => 'or',
'operands' => array(
'operator' => 'EQ',
'operands' => array("region","jp")
)
)
),
'userId' => 'HFEELK3VBE3KPE4MGEA6PZTXXL',
'userIdType' => 'guid'
];
$parameters = json_encode($parameters);
$headers =
[
'Accept: application/json, text/javascript, */*; q=0.01',
'Accept-Language: en-US,en;q=0.5',
'X-Requested-With: XMLHttpRequest',
'Connection: keep-alive',
'Pragma: no-cache',
'Cache-Control: no-cache',
];
$cookie = tmpfile();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.31');
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($parameters));
// curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);
?>
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($parameters));
Change this line. If your problem is that you can send the body through curl then probably you need to send it as a json so try
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($parameters,true));
I saw your comment that you tried to send it already as array and i assume it did not work so i am almost sure that in your post you need to send a json (which is the most common body format for post requests)
Working Example
$data=array();
$data['amount']=100;
$data['to']='test';
$json=json_encode($data);
$header = array('Content-Type: application/json');
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_POST, true);
curl_setopt($handle,CURLOPT_RETURNTRANSFER, 1);
curl_setopt($handle, CURLOPT_POSTFIELDS, $json);
curl_setopt($handle, CURLOPT_HTTPHEADER, $header);
$response=curl_exec($handle);
thank you pr1nc3 and Gurpal singh
i changed this line
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($parameters));
for this one
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($parameters,true));
but i always have this result
'{"error":{"result":null,"error":{"code":"internal-error","description":"STREAMED"}}}'
here again the code
<?php
$url = 'https://query1.finance.yahoo.com/v1/finance/screener?lang=en-US®ion=US&formatted=true&corsDomain=finance.yahoo.com';
// $url = 'https://finance.yahoo.com/screener/unsaved/f0171a68-053e-42f2-a941-d6ecdf2ba6d1';
$parameters =
[
'size' => 25,
'offset' => 50,
'sortField' => 'intradaymarketcap',
'sortType' => 'DESC',
'quoteType' => 'EQUITY',
'topOperator' => 'AND',
'query' => array(
'operator' => 'AND',
'operands'=> array(
'operator' => 'or',
'operands' => array(
'operator' => 'EQ',
'operands' => array("region","jp")
)
)
),
'userId' => 'HFEELK3VBE3KPE4MGEA6PZTXXL',
'userIdType' => 'guid'
];
$headers =
[
'Accept: application/json, text/javascript, */*; q=0.01',
'Accept-Language: en-US,en;q=0.5',
'X-Requested-With: XMLHttpRequest',
'Connection: keep-alive',
'Pragma: no-cache',
'Cache-Control: no-cache',
];
$cookie = tmpfile();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.31');
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($parameters,true));
// curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($parameters));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);
?>
i think the error is in the variable $parameters but i don't see where
when i compare view source send
{"size":25,"offset":0,"sortField":"intradaymarketcap","sortType":"DESC","quoteType":"EQUITY","topOperator":"AND","query":{"operator":"AND","operands":[{"operator":"or","operands":[{"operator":"EQ","operands":["region","jp"]}]}]},"userId":"HFEELK3VBE3KPE4MGEA6PZTXXL","userIdType":"guid"}
with my variable $parameters, there are diferences
{"size":25,"offset":0,"sortField":"intradaymarketcap","sortType":"DESC","quoteType":"EQUITY","topOperator":"AND","query":{"operator":"AND","operands":{"operator":"or","operands":{"operator":"EQ","operands":["region","jp"]}}},"userId":"HFEELK3VBE3KPE4MGEA6PZTXXL","userIdType":"guid"}
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;
I am trying to execute the background REST API Call with Curl
library in php. I guess it is not working.
can you suggest me ?
$cum_url = http://localhost/test/list;
$post = [ 'id' => $object->id ];
$ch = curl_init($cum_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 1);
curl_exec($ch);
curl_close($ch);
UPDATE: Cull error says "Timeout was reached".
Thanks,
Raja K
You need to transfer your post data from array to http parameter string, ex:
$cum_url = "http://localhost/test/list";
$post = [ 'id' => $object->id ];
$postdata = http_build_query($post);
$options = array (CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_AUTOREFERER => true,
CURLOPT_USERAGENT => "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1",
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false);
$ch = curl_init($cum_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt_array ( $ch, $options );
$res = null;
if(!curl_errno($ch)) {
$res = curl_exec($ch);
}
curl_close($ch);
Of course, some option is optional depends on you, ex CURLOPT_USERAGENT. Just show you an example.
I'm quite new in PHP but for practise I wanted to write a script using curl for logging and send post request to a page site written in asp. I've used libcurl and simple_html_dom.php libraries.
Sometimes I got an error for invalid viewstate, this is the stack trace:
[ViewStateException: Invalid viewstate.
Client IP: 111.11.11.111
Port: 4743
User-Agent: Mozilla/5.0
ViewState: /wEPDwUKLTk1OTAzODYwMA9kFgJmD2QWBAIBD2QWAgIBD2QWCmYPFgIeBFRleHQFRTxtZXRhIHByb3BlcnR5PSJvZzp0aXRsZSIgY29udGVudD0iQURJREFTIEVRVCBSVU5OSU5HIENVU0hJT04gOTEiIC8+IGQCAQ8WAh8ABWY8bWV0YSBwcm9wZXJ0eT0ib2c6aW1hZ2UiIGNvbnRlbnQ9Imh0dHA6Ly9zaG9wLnVyYmFuanVuZ2xlc3RvcmUuaXQvZm90b19hcnRpY29saS9kNjc1NjgtaW50cm8uanBnIiAvPiBkAgIPFgIfAAWVBDxtZXRhIHByb3BlcnR5PSJvZzpkZXNjcmlwdGlvbiIgY29udGVudD0iUmVjZW50ZW1lbnRlLCBvbHRyZSBhbGxlIG1pdGljaGUgU3RhbiBTbWl0aCwgc29ubyB0b3JuYXRlIGluIGF1Z2UgZGVpIHBlenppIGRlbGxhIHNlcmllIE9kZGl0eS4gTmVsIDIwMDUgYWRpZGFzIGhhIGxhbmNpYXRvIHVuYSBjb2xsZXppb25lIHNwZWNpYWxlIGRpIHJ1bm5lcnMgY29uIGFsY3VuaSBkZWkgY29sb3JpIHBpJnVncmF2ZTsgcGF6emkgY2hlIHNpIHBvc3Nhbm8gbWV0dGVyZSBzdSB1bmEgc25lYWtlci4gRSBtZW50cmUgcXVlc3RvIG1vZGVsbG8gZGkgYWRpZGFzIEVRVCBSdW5uaW5nIEN1c2hpb24gbm9uIGZhIHVmZmljaWFsbWVudGUgcGFydGUgZGVsIHJpdG9ybm8gZGVsbGEgY29sbGV6aW9uZSBPZGRpdHkgKG8gZm9yc2UgcyZpZ3JhdmU7PyksIGx...]
[HttpException (0x80004005): Validation of viewstate MAC failed. If this application is hosted by a Web Farm or cluster, ensure that <machineKey> configuration specifies the same validationKey and validation algorithm. AutoGenerate cannot be used in a cluster.
http://go.microsoft.com/fwlink/?LinkID=314055]
System.Web.UI.ViewStateException.ThrowError(Exception inner, String persistedState, String errorPageMessage, Boolean macValidationError) +153
System.Web.UI.ViewStateException.ThrowMacValidationError(Exception inner, String persistedState) +14
System.Web.UI.ObjectStateFormatter.Deserialize(String inputString) +237
System.Web.UI.ObjectStateFormatter.System.Web.UI.IStateFormatter.Deserialize(String serializedState) +4
System.Web.UI.Util.DeserializeWithAssert(IStateFormatter formatter, String serializedState) +37
System.Web.UI.HiddenFieldPageStatePersister.Load() +207
System.Web.UI.Page.LoadPageStateFromPersistenceMedium() +199
System.Web.UI.Page.LoadAllState() +43
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +6785
System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +242
System.Web.UI.Page.ProcessRequest() +80
System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context) +21
System.Web.UI.Page.ProcessRequest(HttpContext context) +49
ASP.web_articolo_aspx.ProcessRequest(HttpContext context) +37
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +181
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75
Here is my code:
<?php
include('simple_html_dom.php');
//method for logging
function login($url,$data){
$headers = array(
'Host' => 'site',
'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language' => 'en-US;q=0.5,en;q=0.3',
'Accept-Encoding' => 'gzip, deflate',
'Referer' => 'http://somesite.com',
'Connection' => 'keep-alive',
'Content-Type' => 'application/x-www-form-urlencoded'
);
$login = curl_init();
curl_setopt($login, CURLOPT_COOKIEJAR, dirname(__FILE__) . "/cookie.txt");
curl_setopt($login, CURLOPT_COOKIEFILE, dirname(__FILE__) . "/cookie.txt");
curl_setopt($login, CURLOPT_TIMEOUT, 4000);
curl_setopt($login, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($login, CURLOPT_URL, $url);
curl_setopt($login, CURLOPT_HEADER, 1);
curl_setopt($login, CURLOPT_HTTPHEADER, $headers);
curl_setopt($login, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0');
curl_setopt($login, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($login, CURLOPT_POST, 1);
curl_setopt($login, CURLOPT_POSTFIELDS, $data);
//$verbose = curl_setopt($login, CURLOPT_VERBOSE, true);
$ris = curl_exec ($login);
curl_close($login);
unset($login);
return $ris;
}
//method for posting data
function post_data($site,$data){
$datapost = curl_init();
curl_setopt($datapost, CURLOPT_URL, $site);
curl_setopt($datapost, CURLOPT_TIMEOUT, 4000);
curl_setopt($datapost, CURLOPT_HEADER, 1);
//curl_setopt($datapost, CURLOPT_HTTPHEADER, $headers);
curl_setopt($datapost, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0');
curl_setopt($datapost, CURLOPT_POST, 1);
curl_setopt($datapost, CURLOPT_POSTFIELDS, $data);
curl_setopt($datapost, CURLOPT_COOKIEFILE, dirname(__FILE__) . "/cookie.txt");
$result = curl_exec ($datapost);
curl_close ($datapost);
return $result;
}
function getParamLogin($url,$pageType) {
//return the html page
$html = file_get_html($url);
$eventtarget = '';
$eventargument = '';
if(sizeof($html->find('[id=__EVENTTARGET]')) > 0)
$eventtarget = $html->find('[id=__EVENTTARGET]')[0]->value;
if(sizeof($html->find('[id=__EVENTARGUMENT]')) > 0)
$eventargument = $html->find('[id=__EVENTARGUMENT]')[0]->value;
//post params
$params = array('ctl00_ScriptManager1_HiddenField' => $html->find('[id=ctl00_ScriptManager1_HiddenField]')[0]->value,
'__EVENTTARGET' => $eventtarget,
'__EVENTARGUMENT' => $eventargument,
'__VIEWSTATE' => $html->find('[id=__VIEWSTATE]')[0]->value,
'__VIEWSTATEGENERATOR' => $html->find('[id=__VIEWSTATEGENERATOR]')[0]->value,
'__EVENTVALIDATION' => $html->find('[id=__EVENTVALIDATION]')[0]->value,
'ctl00$search' => '',
'ctl00$newsletter' => ''
);
switch ($pageType) {
case 'firstpage':
$login = array('ctl00$pagina$tlogin_area' => 'user',
'ctl00$pagina$tpassword_area' => 'passwd',
'ctl00$pagina$tinvia' => 'ACCEDI',);
$params = array_merge($params,$login);
break;
case 'secondpage':
$cart = array('__SCROLLPOSITIONX' => '0',
'__SCROLLPOSITIONY' => '0',
'ctl00$pagina$taglie' => '6M',
'ctl00$pagina$tag' => 'Aggiungi al carrello');
$params = array_merge($params,$cart);
break;
}
print_r($params);
return $params;
}
?>
<?php
login('http://www.loginpage.com', getParamLogin('http://www.loginpage.com','firstpage'));
post_data('http://www.anotherpage.com',getParamLogin('http://www.anotherpage.com','secondpage'));
?>
I wonder if there is also a cookie in handle the cookie and then in the viewstate... Someone can help me? Thank you!
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