I'm making a request to the google closure compiler API service:
$content = file_get_contents('file.js');
$url = 'http://closure-compiler.appspot.com/compile';
$post = true;
$postData = array('output_info' => 'compiled_code', 'output_format' => 'text', 'compilation_level' => 'SIMPLE_OPTIMIZATIONS', 'js_code' => urlencode($content)));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if ($post) {
curl_setopt($ch, CURLOPT_POST, $post);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
}
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded; charset=UTF-8'));
But the request is failing and I get this error message from google:
Error(18): Unknown parameter in Http request: '------------------------------0f1f2f05fb97
Content-Disposition: form-data; name'.
Error(13): No output information to produce, yet compilation was requested.
I looked at the headers and this Content-Type header is being sent:
application/x-www-form-urlencoded; charset=UTF-8; boundary=----------------------------0f1f2f05fb97
Not sure if that added boundary is normal? And how do I prevent this as google doesn't seem to like it?
Thank you,
Wesley
You have to use http_build_query() prior to sending POST data (array) to cURL.
string http_build_query ( mixed $query_data [, string $numeric_prefix [, string $arg_separator [, int $enc_type = PHP_QUERY_RFC1738 ]]] )
So your $postData should look like this:
$postData = http_build_query(
array(
'output_info' => 'compiled_code',
'output_format' => 'text',
'compilation_level' => 'SIMPLE_OPTIMIZATIONS',
'js_code' => urlencode($content)
)
);
Looks like Google's API doesn't support multipart/form-data data. Which seems a bit lame to me...
According to the PHP documentation on curl_setopt():
Passing an array to CURLOPT_POSTFIELDS will encode the data as multipart/form-data,
while passing a URL-encoded string will encode the data as application/x-www-form-urlencoded.
So it should work if you change the 4th line of your code to something like this:
$postData = 'output_info=compiled_code&output_format=text&compilation_level=SIMPLE_OPTIMIZATIONS&js_code=' . urlencode($content);
In other words, you have to do the URL encoding yourself - you apparently can't rely on cURL to take an array and encode it for you.
1.) Don't use an array() to avoid switch to multipart/form-data:
Passing an array to CURLOPT_POSTFIELDS will encode the data as
multipart/form-data, while passing a URL-encoded string will encode
the data as application/x-www-form-urlencoded.
2.) Don't use http_build_query() to avoid double encoding problems (#Wesley). In addition you aren't able to use keys twice (I know this workaround, but its ugly).
Some of my error messages by using http_build_query():
JSC_PARSE_ERROR Input_0
Parse error. illegal octal literal digit 9; interpreting it as a decimal digit
Parse error. syntax error
ERROR - Parse error. missing ( before function parameters
3.) My proposal:
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Expect:',
'Content-type: application/x-www-form-urlencoded',
));
curl_setopt($ch, CURLOPT_TIMEOUT, 30); // don't set it to low! sending and response needs time
curl_setopt($ch, CURLOPT_ENCODING, ''); // automatically sets supported encodings
//curl_setopt($ch, CURLOPT_HEADER, true); // for debugging response header
//curl_setopt($ch, CURLINFO_HEADER_OUT, true); // for debugging request header
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // false would echo the answer
curl_setopt($ch, CURLOPT_POST, true);
// settings
curl_setopt($ch, CURLOPT_POSTFIELDS,
'output_format=json'
.'&output_info=compiled_code'
.'&output_info=warnings'
.'&output_info=errors'
.'&output_info=statistics'
.'&compilation_level=ADVANCED_OPTIMIZATIONS'
.'&warning_level=verbose'
//.'&output_file_name=default.js'
//.'&code_url='
.'&js_code=' . urlencode($js_code)
);
curl_setopt($ch, CURLOPT_URL, 'http://closure-compiler.appspot.com/compile');
$response = curl_exec($ch);
//$response = curl_getinfo($ch, CURLINFO_HEADER_OUT) . $response; // for debugging request header
print_r(json_decode($response, true));
Related
I have integrated SMS gateway and using curl functions to send SMS. In the text which I am sending I have a '#' symbol because of which curl function is failed.
My text is
$smsbody = "A new user has logged in\nEmail: testemail#gmail.com";
Below is my code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{
\"Text\": \"$smsbody\",
\"Number\": \"$tomobile\",
\"SenderId\": \"Example\",
\"DRNotifyUrl\": \"https://www.example.com\",
\"DRNotifyHttpMethod\": \"POST\",
\"Tool\": \"API\"
}");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: application/json",
"Authorization: ".$smsauthor
));
$response = curl_exec($ch);
curl_close($ch);
If I remove '#' symbol then I am able to send message successfully. For the messages with '#' symbol, message is getting failed.
How to encode the symbol '#' or what could be the alternative for this problem?
Use curl_escape ( resource $ch , string $str ). Docs
This function URL encodes the given string according to » RFC 3986.
Using:
<?php
// Create a curl handle
$ch = curl_init();
// Escape a string used as a GET parameter
$location = curl_escape($ch, 'Hofbräuhaus / München');
// Result: Hofbr%C3%A4uhaus%20%2F%20M%C3%BCnchen
// Compose an URL with the escaped string
$url = "http://example.com/add_location.php?location={$location}";
// Result: http://example.com/add_location.php?location=Hofbr%C3%A4uhaus%20%2F%20M%C3%BCnchen
// Send HTTP request and close the handle
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
curl
_close($ch);
?>
I'm trying to use Application Only Authentication, as described here:
https://developer.twitter.com/en/docs/basics/authentication/overview/application-only
I'm using the following PHP code to do so.
if(empty($_COOKIE['twitter_auth'])) {
require '../../social_audit_config/twitter_config.php';
$encoded_key = urlencode($api_key);
$encoded_secret = urlencode($api_secret);
$credentials = $encoded_key.":".$encoded_secret;
$encoded_credentials = base64_encode($credentials);
$request_headers = array(
'Host: api.twitter.com',
'User-Agent: BF Sharing Report',
'Authorization: Basic '.$encoded_credentials,
'Content-Type: application/x-www-form-urlencoded;charset=UTF-8',
'Content-Length: 29',
'Accept-Encoding: gzip'
);
print_r($request_headers);
$ch = curl_init();
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, 'https://api.twitter.com/oauth2/token');
curl_setopt($ch, CURLOPT_POSTFIELDS, 'grant_type=client_credentials');
$attempt_auth = curl_exec($ch);
print_r($attempt_auth);
}
It should return JSON with the token in it, but instead it returns gobbledygook, as seen in the image below:
I'm sure I'm missing some very simple step, where am I going wrong?
If I send the curl request without the headers, it returns an error in JSON format as expected, so is there something wrong with my headers?
You have few options here. Instead of setting header directly, use below
curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
If you set header directly then you should use
print_r(gzdecode($attempt_auth));
See below thread as well
Decode gzipped web page retrieved via cURL in PHP
php - Get compressed contents using cURL
trying to make an API request via curl. The API docs say I must make a POST request as follows:
POST url
Headers:
Content-Type: “application/json”
Body:
{
Context: {
ServiceAccountContext: "[Authorization Token]"
},
Request:{
Citations:[
{
Volume: int,
Reporter: str,
Page: int
}
]
}
}
Here is my curl request:
$postFields = array(
'Volume' => int,
'Reporter' => str,
'Page' => int,
'ServiceAccountContext' => $API_KEY
);
$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_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:application/json"));
curl_setopt($ch, CURLOPT_POST, count($postFields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
$output=curl_exec($ch);
But the API is not recognizing that I have submitted the API_KEY through a POST Field. The error I am getting back is to create a SecurityContext object, which I assume is something to do with the part of the POST body talking about Context and ServiceAccountContext.
I have looked in the cURL documentation and have not seen how I can set this. Any suggestions? Thanks a bunch.
The problem is that you use CURL options improperly. According to manual, when you set CURLOPT_POSTFIELDS option to an array, CURL forces Content-Type header to multipart/form-data. i.e. the line where you set CURLOPT_HTTPHEADER option is ignored.
You have to convert $postFields to JSON string by json_encode function before passing it to CURLOPT_POSTFIELDS option:
...
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:application/json"));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postFields));
...
I am using below post method for google account using curl but it gives me invalid_request error.
POST /o/oauth2/token HTTP/1.1
Host: accounts.google.com
Content-Type: application/x-www-form-urlencoded
code=4/ux5gNj-_mIu4DOD_gNZdjX9EtOFf&
client_id=1084945748469-eg34imk572gdhu83gj5p0an9fut6urp5.apps.googleusercontent.com&
client_secret=CENSORED&
redirect_uri=http://localhost/oauth2callback&
grant_type=authorization_code
Here is my PHP code with curl
$text ='test';
$URL = "https://accounts.google.com/o/oauth2/token";
$header = array(
"POST /o/oauth2/token HTTP/1.1",
"Host: accounts.google.com",
"Content-type: application/atom+xml;charset=\"utf-8\"",
"Accept: text/xml",
"Cache-Control: no-cache",
"code=[my_code]&client_id=[my_client_id]&client_secret=[my_client_secret]& redirect_uri=http://localhost/curl_resp.php&grant_type=authorization_code",
"Content-length: ".strlen($text),
);
$xml_do = curl_init();
curl_setopt($xml_do, CURLOPT_URL, $URL);
curl_setopt($xml_do, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($xml_do, CURLOPT_TIMEOUT, 10);
curl_setopt($xml_do, CURLOPT_RETURNTRANSFER, true);
curl_setopt($xml_do, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($xml_do, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($xml_do, CURLOPT_POST, false);
curl_setopt($xml_do, CURLOPT_POSTFIELDS, $text);
curl_setopt($xml_do, CURLOPT_HTTPHEADER, $header);
And I am having invalid request error
I don't know anything about using Google's oAuth API, but from the examples that I have looked at so far, it looks like you are supposed to pass the values (i.e. code, client_id, etc.) in the post fields, not directly in the HTTP header.
The following example still doesn't work completely, but instead of getting a invalid_request error, it gives you invalid_grant. I think there is something else wrong in addition to what I've mentioned (perhaps you need new credentials from Google or something), but this might get you one step closer, at least:
$post = array(
"grant_type" => "authorization_code",
"code" => "your_code",
"client_id" => "your_client_id",
"client_secret" => "your_client_secret",
"redirect_uri" => "http://localhost/curl_resp.php"
);
$postText = http_build_query($post);
$url = "https://accounts.google.com/o/oauth2/token";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postText);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$result = curl_exec($ch);
var_dump($result); // gets an error, "invalid_grant"
The response to your request actually includes a very readable description of the problem:
POST requests require a Content-length header.
Why do you have CURLOPT_POST set to false?
curl_setopt($xml_do, CURLOPT_POST, false);
Although some configurations of cURL will automatically change this to tru if CURLOPT_POSTFIELDS is set, you do not have a valid postfield.
The full data to post in a HTTP "POST" operation. To post a file, prepend a filename with # and use the full path. The filetype can be explicitly specified by following the filename with the type in the format ';type=mimetype'. This parameter can either be passed as a urlencoded string like 'para1=val1¶2=val2&...' or as an array with the field name as key and field data as value. If value is an array, the Content-Type header will be set to multipart/form-data. As of PHP 5.2.0, value must be an array if files are passed to this option with the # prefix.
It should either be in param=value syntax or an array passed.
I have used post array in CURL method and valid Google code and it worked for me
I was also having the same problem, where google returns an "invalid_xxx".
After much researching and troubleshooting, the problem lies with the encoding of the form itself! Do not use function 'http_builder_query', as it messses up the string and google cannot 'recognise' it. Example :
http_builder_query Output : "code=4%252FYYUT71KJ6..."
compared to
just a normal string : "code=4/YYUT71KJ6..."
Here is my working code, where i have placed 'x' in the locations that needs your own data (taken and modified from google oauth authentication)
$code_from_user_login = "4/YYUT71KJ6....";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://accounts.google.com/o/oauth2/token");
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$post_params = "code=" . $code_from_user_login . "&";
$post_params .= "redirect_uri=http://www.x.com/&";
$post_params .= "client_id=x.apps.googleusercontent.com&";
$post_params .= "client_secret=x&";
$post_params .= "grant_type=authorization_code&";
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_params);
$result = curl_exec($ch);
print($result);
The result will be like :
'{
"access_token" : "ya29.AHES6ZSwJSHpTZ1t....",
"token_type" : "Bearer",
"expires_in" : 3599,
"id_token" : "eyJhbGciOiJSUzI1NiIsImtpZCI6IjU0MDQxOTZlMmQzOGNjYTA2MW...."
}'
Once you have the 'access_token', access the profile data with this url :
https://www.googleapis.com/oauth2/v1/userinfo?access_token=YOUR_ACCESS_TOKEN_HERE
~ end ~
I'm working with the Google Translate API and there's the possibility that I could be sending in quite a bit of text to be translated. In this scenerio Google recommends to do the following:
You can also use POST to invoke the API if you want to send more data
in a single request. The q parameter in the POST body must be less
than 5K characters. To use POST, you must use the
X-HTTP-Method-Override header to tell the Translate API to treat the
request as a GET (use X-HTTP-Method-Override: GET). Google Translate API Documentation
I know how to make a normal POST request with CURL:
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($curl);
curl_close($curl);
echo $response;
But how do I modify the header to use the X-HTTP-Method-Override?
curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-HTTP-Method-Override: GET') );
http://php.net/manual/en/function.curl-setopt.php
CURLOPT_HTTPHEADER
An array of HTTP header fields to set, in the format array('Content-type: text/plain', 'Content-length: 100')
Thus,
curl_setopt($curl, CURLOPT_HTTPHEADER, array('X-HTTP-Method-Override: GET'));
use the CURLOPT_HTTPHEADER option to add a header from a string array
Not enough for me , i need to use http_build_query fo my array post data
my full example :
$param = array(
'key' => 'YOUR_API_KEY_HERE',
'target' => 'en',
'source' => 'fr',
"q" => 'text to translate'
);
$formData = http_build_query($param);
$headers = array( "X-HTTP-Method-Override: GET");
$ch=curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$formData);
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers );
curl_setopt($ch, CURLOPT_REFERER, 'http://yoursite'); //if you have refere domain restriction for your google API KEY
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,'https://www.googleapis.com/language/translate/v2');
$query = curl_exec($ch);
$info = curl_getInfo($ch);
$error = curl_error($ch);
$data = json_decode($query,true);
if (!is_array($data) || !array_key_exists('data', $data)) {
throw new Exception('Unable to find data key');
}
if (!array_key_exists('translations', $data['data'])) {
throw new Exception('Unable to find translations key');
}
if (!is_array($data['data']['translations'])) {
throw new Exception('Expected array for translations');
}
foreach ($data['data']['translations'] as $translation) {
echo $translation['translatedText'];
}
I found this help here https://phpfreelancedeveloper.wordpress.com/2012/06/11/translating-text-using-the-google-translate-api-and-php-json-and-curl/
Hope that helps