PHP cURL HTTP Code 0 in Google App Engine - php

I am using curl in the Google App Engine (GAE) environment. My code simplified is as follows
$url = 'https://img.youtube.com/vi/rzuYnQwD840/0.jpg';
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($handle);
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
As long as $url is valid, one would expect $httpCode to be 200. This is what I get testing this code locally. This code was also working last month on GAE in production last month when it was pushed. Today we noticed that $httpCode is now coming up as 0 on GAE. Is there some way to fix this? I just need to test if the response to a URL request is 200.
Update: It looks like the issue is limited to YouTube? There doesn't appear to be a problem with Vimeo (e.g. $url = https://vimeo.com/api/v2/video/149497499.json';).

First Method:
Replace $url with 'http://img.youtube.com/vi/rzuYnQwD840/0.jpg';
This problem occurs due to SSL. You are hitting a secure URL.
Second Method:
Use the following code:
$url = 'https://img.youtube.com/vi/rzuYnQwD840/0.jpg';
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($handle);
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);

So I found the answer I was looking for in another GAE question (PHP's Curl not working on AppEngine). Within the php.ini file in the base directory of the application, add the following:
google_app_engine.enable_curl_lite = "1"
I'm using Apache for local development, but if you're using GAE locally then add the following
extension = "curl.so"
NB You can only have one of these in you php.ini file at a time. It will ignore whichever one comes second.

Related

API I'm using can't be accessed unless Captcha is completed

I am trying to access API data from CraftingStore Public API.
When I am trying it on localhost, everything works like it should (1st pic, also printing results for to show).
However, when I am trying to view it on the actual website (2nd pic), it is not working but instead saying to enable cookies and to complete captcha.
When on localhost:
When on website:
Also code for accessing the API:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.craftingstore.net/v7/payments?page=1');
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'token:'.$cs_token
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
$obj = json_decode($result);
$dononame = $obj->data[0]->inGameName;
$donobuy = $obj->data[0]->packageName;
What can I do?
This is quite usual for API's with Cloudflare. You'll need to make a page rule for the API domain/URI to disable the Browser Integrity Check.
Source: https://support.cloudflare.com/hc/en-us/articles/200504045-Using-Cloudflare-with-your-API

cURL request getting wrong request version in Google App Engine

I have a cURL request in my code which works fine when running locally:
$url = "http://ipinfo.io/{$_SERVER['REMOTE_ADDR']}";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$response = curl_exec($ch);
$locale = json_decode($response);
and returns a JSON as expected. Our production system is on Google App Engine, however, where I get the website version for a browser rather than the JSON.
I can get this cURL request to work if I change
google_app_engine.enable_curl_lite = "1"
in the php.ini in the root directory of my project to
extension = "curl.so"
but Google's documentation insists the former is to be used on production. Additionally, using the latter breaks things like Monolog's SlackHandler.
Is there a way to get the JSON from this cURL request while still using Google's "cURL Lite"?
From the ipinfo.io documentation:
"We do a little bit of magic on the server to determine if we should send the JSON response or the webpage. We usually get it right, but if you're seeing the webpage instead of the JSON (or want to check the JSON output in a browser) you can force the JSON response by adding /json to the end of the URL"
Adding /json to the end of the URL worked for me in this case, but I wanted a more general solution. Since Google's cURL Lite uses their URL Fetch in the background, ipinfo.io's "magic" is somehow getting confused. I found that by specifying the Accept header then the /json addition wasn't required. In PHP:
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
Thanks to the folks on the #php channel of NashDev Slack for helping me on this one!

Proxy blocking file_get_contents

In my business I have to use google map for my application (calculated distance)
We currently use a configuration script for proxy.
In my application I use the method to query file_get_contents Google Map.
$url = 'http://maps.google.com/maps/api/directions/xml?language=fr&origin='.$adresse1.'&destination='.$adresse2.'&sensor=false';;
$xml=file_get_contents($url);
$root = simplexml_load_string($xml);
$distance=$root->route->leg->distance->value;
$duree=$root->route->leg->duration->value;
$etapes=$root->route->leg->step;
return array(
'distanceEnMetres'=>$distance,
'dureeEnSecondes'=>$duree,
'etapes'=>$etapes,
'adresseDepart'=>$root->route->leg->start_address,
'adresseArrivee'=>$root->route->leg->end_address
);
}
But with the proxy I have an unknown host error. (I tested my home, the code works fine). I wanted to know if there is a way to take into account the proxy that I identify myself as when I browse the web?
You can do this with cURL. It's a bit more verbose than a simple call to file_get_contents(), but a lot more configurable:
$url = 'http://maps.google.com/maps/api/directions/xml?language=fr&origin='.$adresse1.'&destination='.$adresse2.'&sensor=false';
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_PROXY, ''); // your proxy address (and optional :port)
curl_setopt($handle, CURLOPT_PROXYUSERPWD, ''); // credentials in username:password format (if required)
curl_setopt($handle, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);
$xml = curl_exec($handle);
curl_close($handle);
//continue logic using $xml as before
the google have restrictions to number of queries per time segment for it's api
at first u must cache results on your side, and add a pause between queries
https://developers.google.com/maps/documentation/business/articles/usage_limits
Of course you can also mention a "context" to make file_get_contents recognize a proxy. Please see my own question and my own answer to that question here

cURL call to web service not working

I am trying to make a simple get call to a .NET RESTful web service at https://salesgenie.com/brandingservice/details?url=att.salesgenie.com The test page making the cURL call is at https://test-cms.salesgenie.com/wp-content/themes/salesgenie/branding-proxy.php The web service can be called in the browser but is timing out when called via cURL. Here is the code from the branding-proxy.php page:
if (!function_exists('curl_init')){
die('Sorry cURL is not installed!');
}
$url = 'https://salesgenie.com/brandingservice/details?url=att.salesgenie.com';
$handle = curl_init();
curl_setopt($handle, CURLOPT_URL, $url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($handle, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($handle, CURLOPT_SSLVERSION,3);
$response = curl_exec($handle);
$code = curl_getinfo($handle, CURLINFO_HTTP_CODE);
curl_close($handle);
setcookie('CmsCookie', $code . ' ' . $response, 0, '/', 'salesgenie.com', 0);
echo 'Code:' . $code."<br />Response: ".$response;
I can see that the timeout happens while using your links, though I think the problem happens somewhere else. The reason I've come to that conclusion is that I uploaded your code to my own website. It worked fine.
Important
If you are the owner of salesgenie.com then I think you have a problem with the server, since sometimes the responses I got was either a timeout (code 0), code 200 (with result) or code 320. So the problem might be a problem within the server itself.
Tricks
Try removing the setcookie to spare some processing.
Also try using http instead of https since https requires some more processing to use.

How to provide Google Maps API key with php and curl

I am trying to develop a small client to geocode the address and display it on the map. It all works fine, but I wanted to provide the api key so I can check the number of requests. I am using curl and php.
Here is the code:
$url = 'http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=GIVEN_ADDRESS&key=API_KEY';
$client = curl_init();
curl_setopt($client, CURLOPT_URL, $url);
curl_setopt($client, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($client);
//$http_status = curl_getinfo($client, CURLINFO_HTTP_CODE);
curl_close($client);
When I try this i get request denied from google.
any suggestions?
please provide full URL of request or use this one for test http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=USA (works for me)

Categories