PHP cURL response error when comparing in if statement - php

Ok, this literally makes no sense...
I am using this function to call an API
function get_web_page($url) {
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
CURLOPT_ENCODING => "", // handle compressed
CURLOPT_USERAGENT => "API", // name of client
CURLOPT_AUTOREFERER => true, // set referrer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // time-out on connect
CURLOPT_TIMEOUT => 120, // time-out on response
);
$ch = curl_init($url);
curl_setopt_array($ch, $options);
$content = curl_exec($ch);
curl_close($ch);
return $content;
}
Which works find if i do something like this
$exampleResponse = get_web_page("https://example.com/index.php?example=1");
echo $exampleResponse;
But what makes absolutely no sense (to me) is that trying to compare the response in a if statement DOES NOT WORK! Here is an example (assume the API response is "example")
$exampleResponse = get_web_page("https://example.com/index.php?example=1");
if($exampleResponse == "example")
{
echo "ok";
}
This will not work and ok will NOT be echoed even though the API responded with "example". This is very confusing to me, if anyone has any idea why this may be occurring please let me know - thanks for your time.

Related

Get HTTP response body content from site for parsing with PHP

I know there are lots of articles on how to get response with Curl and then decode it but non of them worked. Sending request with CURL. Getting response. Dumping response content on the screen shows only "String(996)". I guess its compressed string. Tried to decompress it but didnt work.
Here is my case if someone can help me with that.
I tried this code with different url and it worked.
<?php
//set up variables
$url = 'https://en.bidfax.info';
$response = get_web_page($url);
var_dump ($response);
function get_web_page($url) {
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
CURLOPT_SSL_VERIFYPEER => false // Disabled SSL Cert checks
);
$ch = curl_init($url);
curl_setopt_array($ch, $options);
$content = curl_exec($ch);
curl_close($ch);
return $content;
}
Went through all posts about curl and gzdecode / encode.

Square API Won't Return List of Locations - Invalid Request Error

Here is my code:
// Build request URL
$url = 'https://connect.squareup.com/v2/locations/';
// Build and execute CURL request
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
CURLOPT_ENCODING => "", // handle compressed
CURLOPT_AUTOREFERER => true, // set referrer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // time-out on connect
CURLOPT_TIMEOUT => 120, // time-out on response
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer ' . $accessToken,
'Accept: application/json',
)
);
$ch = curl_init($url);
curl_setopt_array($ch, $options);
$content = curl_exec($ch);
curl_close($ch);
var_dump($content);
Here is what I get back:
string(158) "{"errors":[{"category":"INVALID_REQUEST_ERROR","code":"NOT_FOUND","detail":"API endpoint for URL path `/v2/locations/` and HTTP method `GET` is not found."}]}"
I am pounding my head on this one... I tried using the Square SDK but calling from it doesn't return a list of locations either.
I have created an application on the Square Developer Dashboard. $accessToken is set to the sandbox access token listed there.
You have added an extra / at the end of the url. You should instead use:
$url = 'https://connect.squareup.com/v2/locations';
Other than that your code works!

PHP: Curl not called URL and got Error No =0

I am developing one SMS application. I have to call one link
for eg http://sendsms.com/send.php?mobile=45455&msg=hello for sending SMS
so I used CURL concept to send sms.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 15, // timeout on connect
CURLOPT_TIMEOUT => 15, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
);
curl_setopt_array($ch, $options);
$content = curl_exec($ch);
$err = curl_errno($ch);
$errmsg = curl_error($ch);
$header = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
$output ="Error:".$err." ErrMsg:".$errmsg." Header:". json_encode($header);
The Problem is message is not sending. I checked the SMS API server. And There also no request is received. So the only problem is CURL may not calling the server. I tried to print the Error msg and I got curl_errno($ch) is 0
please provide me the best way to do this
Try to send request by POST:
curl_setopt($ch, CURLOPT_POST ,1);

How to parse a web site that uses `__dopostback` ajax call with curl and php

Before someone says something or "jump," I would like to say that I have read:
Any idea on how to scrape pages which are behind __doPostBack('...');?
DotNetNuke, PHP, Simulating a remote postback using curl
Scraping HTML with JavaScript postbacks
cURL post data to asp.net page
http://techclimber.blogspot.com.es/2009/03/php-curl-and-aspnet.html
This is my function:
function UolgetHtmlfromAjaxCallback($a_Params,$url) {
$EVENTTARGET = $this->UolgetAtributoEventTarget($a_Params['s_EventTarget']);
$s_smMaster = 'ctl00$cphSite$upModelo|'.$entries['target'];
$VIEWSTATE = urlencode($a_Params['s_ViewState']);
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "spider", // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => 'ct100%24smMaster='.urldecode($s_smMaster).
'&__EVENTTARGET='.urlencode($EVENTTARGET['target']).
'&__EVENTARGUMENT='.urlencode('').
'&__EVENTVALIDATION='.urlencode($s_Eventvalidation).
'&__VIEWSTATE='.$VIEWSTATE.
'&ct100%24txtBuscaNome='.$a_Params['ctl00_txtBuscaNome'].
'&__ASYNCPOST=true'
);
$ch = curl_init( $url );
curl_setopt_array( $ch, $options );
$result = curl_exec ($ch);
return $result;
}
The $VIEWSTATE is okay, and the $EVENTTARGET is an array with these values:
Array (
[id] => ctl00_cphSite_fichaTecnicaEditorial_rptCarrosFichaTecnica_ctl00_lbtnFichaTecnica
[target] => ctl00$cphSite$fichaTecnicaEditorial$rptCarrosFichaTecnica$ctl00$lbtnFichaTecnica )
I'm trying to use this code to scrape this website:
http://comparecar.uol.com.br/Modelo/Volvo-Xc60
And I get the website, but not the car's information.
I edit my own information.
I'm working with Tamper Data and I found that the post uses different parameters:
ctl00%24smMaster
__EVENTTARGET
__EVENTARGUMENT
__VIEWSTATE
__EVENTVALIDATION
ctl00%24txtBuscaNome
__ASYNCPOST
And closes with an "="

PHP cheapest way how to check if remote image exist

I want to check if a remote image exist.
Currently using CURL and based on its response to i can determine if the file exist.
However, this takes a fare bit amount of time depending on the image file size also.
Is there a safest and cheapest way of doing a check without doing it offline or using cache?
$ch = curl_init();
// set URL and other appropriate options
/*--Note: if in sabre proxy, please activate the last 4 lines--*/
$options = array( CURLOPT_URL => $img,
CURLOPT_HEADER => false, // get the header
CURLOPT_NOBODY => true, // body not required
CURLOPT_RETURNTRANSFER => false, // get response as a string from curl_exec, not echo it
CURLOPT_FRESH_CONNECT => false, // don't use a cached version of the url
CURLOPT_FAILONERROR => true,
CURLOPT_TIMEOUT => 5
);
curl_setopt_array($ch, $options);
if(!curl_exec($ch)) { return FALSE; }
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return ($httpcode<400);
Perform a HEAD request and check the response code.

Categories