I have this command-line cURL example:
curl -X PUT -H "X-Auth-Token: $AUTH_TOKEN" \
-d "{\"data\":{\"name\":\"Device1 Callflow\", \"numbers\":[\"1001\"], \"flow\":{\"module\":\"device\",\"data\":{\"id\":\"$DEVICE_ID\"}}}}" \
http://ip.add.re.ss:8000/v2/accounts/$ACCOUNT_ID/callflows | python -mjson.tool
My task is to convert this into a PHP function, where the needed values are passed into the function:
function someFunc($AUTH_TOKEN, $ACCOUNT_ID, $DEVICE_ID, $numbers) {}
Normally, I use this sort of code:
function setLimits($auth_token, $accountID, $feature1_cnt, $feature2_cnt) {
$service_url = "http://ip.add.re.ss:8000/v2/accounts/$accountID/limits";
$data = '{"data":{"feature1": ' . $feature1_cnt . ',"feature2": ' . $feature2_cnt . '}}';
$ch = curl_init($service_url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-Auth-Token: $auth_token",'Content-Type: application/json', 'Content-Length: ' . strlen($data)));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if ($response === false) {
$info = curl_getinfo($ch);
curl_close($ch);
die('error occured during curl exec. Additional info: ' . var_export($info));
}
$decoded = json_decode($response);
if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') {
die('error occured: ' . $decoded->response->errormessage);
}
return(true);
}
BUT, for a specific API call, I need to pass this as the service URL:
http://ip.add.re.ss:8000/v2/accounts/$ACCOUNT_ID/callflows | python -mjson.tool
How do I do that?
First, change:
$data = '{"data":{"feature1": ' . $feature1_cnt . ',"feature2": ' . $feature2_cnt . '}}';
to:
$data = [
"data" => [
"feature1" => $feature1_cnt,
"feature2" => $feature2_cnt
]
];
You will make a mistake typing out JSON, and it won't be easy to find or fix.
Second, python -mjson.tool
json.tool [...] to validate and pretty-print:
PHP's JSON validation is pretty much just checking if json_decode() returned NULL, and then you can check json_last_error() and json_last_error_msg() for error info. It is not robust.
You can encode JSON in "pretty-printed" format with:
json_encode($foo, JSON_PRETTY_PRINT);
But I can't think of an earthly reason why you would want to do that other than if there are human eyeballs that are going to be looking at it.
Lastly, in answering your question I've realized that I have no actual idea what you're getting at and you really need to clarify why you think you need to do anything you asked about. [other than the validation, I suppose]
Related
I have the following curl request to GraphQL. It works great, but in production shell_exex is not allowed. How do I re-write this curl post in valid PHP?
$curl_string = 'curl -g -X POST -H "Content-Type: application/json" -H "Authorization: Bearer "' . AIRTABLE_API_KEY;
$curl_second_string = ' -d \'{"query": "{fullCapaReview (id: \"' . $id . '\") {proposedRuleName submissionDate agencyContactName statusLawDept}}"}\' https://api.baseql.com/airtable/graphql/appXXXzzzzzzzzzz';
$curl_complete_string = "$curl_string $curl_second_string";
$result = shell_exec($curl_complete_string);
edit: I'm sorry, I put the wrong query. The query I had in mind was:
' -d \'{"query": "{dMsAgencies (agencyAcronym: \"' . $_agency . '\") {agencyAcronym fullCapaReview { id }}}"}\'
I make two similar calls. I will leave the original there because someone answered based on that.
This is what I have so far:
$curl = curl_init($url);
$query = 'query dMsAgencies($agencyAcronym: String) {agencyAcronym fullCapaReview { id }} ';
$variables = ["agencyAcronym" => $id ];
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode(['query' => $query, 'variables' => $variables]));
curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: application/json','Authorization: Bearer ' . AIRTABLE_API_KEY]);
$response = curl_exec($curl);
curl_close($curl);
console_log("Response : " . $response);
This is the error message I am getting. I just want to see if I am in the ballpark with my syntax.
Response : {"errors":[{"message":"Cannot query field \"agencyAcronym\" on type \"Query\".","locations":[{"line":1,"column":44}],"stack":["GraphQLError: Cannot query field \"agencyAcronym\" on type \"Query\"."," at Object.Field (/var/app/current/node_modules/graphql/validation/rules/FieldsOnCorrectTypeRule.js:46:31)"," at Object.enter (/var/app/current/node_modules/graphql/language/visitor.js:323:29)"," at Object.enter (/var/app/current/node_modules/graphql/utilities/TypeInfo.js:370:25)"," at visit (/var/app/current/node_modules/graphql/language/visitor.js:243:26)"," at validate (/var/app/current/node_modules/graphql/validation/validate.js:69:24)"," at graphqlMiddleware (/var/app/current/node_modules/express-graphql/index.js:133:32)"," at processTicksAndRejections (internal/process/task_queues.js:95:5)"]},{"message":"Variable \"$agencyAcronym\" is never used in operation \"dMsAgencies\".","locations":[{"line":1,"column":19}],"stack":["GraphQLError: Variable \"$agencyAcronym\" is never used in operation \"dMsAgencies\"."," at Object.leave (/var/app/current/node_modules/graphql/validation/rules/NoUnusedVariablesRule.js:38:33)"," at Object.leave (/var/app/current/node_modules/graphql/language/visitor.js:344:29)"," at Object.leave (/var/app/current/node_modules/graphql/utilities/TypeInfo.js:390:21)"," at visit (/var/app/current/node_modules/graphql/language/visitor.js:243:26)"," at validate (/var/app/current/node_modules/graphql/validation/validate.js:69:24)"," at graphqlMiddleware (/var/app/current/node_modules/express-graphql/index.js:133:32)"," at processTicksAndRejections (internal/process/task_queues.js:95:5)"]}]}
message":"Cannot query field \"agencyAcronym\" on type \"Query\"
{"message":"Variable \"$agencyAcronym\" is never used in operation \"dMsAgencies\".","locations":[{"line":1,"column":19}]
The queries are not the same, but assuming that you are aware, your PHP example also has a syntax issue.
query dMsAgencies($agencyAcronym: String) {
agencyAcronym
fullCapaReview {
id
}
}
If you compare this with the example in the docs (when using variables) you can see that you are currently not using the $agencyAcronym variable anywhere (and there probably isn't a query named agencyAcronym in your schema). Here is one example (using the query from your first snippet):
query dMsAgencies($agencyAcronym: String) {
fullCapaReview (id: $agencyAcronym) {
proposedRuleName
submissionDate
agencyContactName
statusLawDept
}
}
I just started using the BING translate API to do a small volume of translations into most of their supported languages and that works pretty well.
There is a GitHub project that has simple PHP code for making the API call to Microsoft. You mostly just need the API key, and it can be customized pretty easily.
Text-Translation-API-V3-PHP
// NOTE: Be sure to uncomment the following line in your php.ini file.
// ;extension=php_openssl.dll
// **********************************************
// *** Update or verify the following values. ***
// **********************************************
// Replace the subscriptionKey string value with your valid subscription key.
$key = 'ENTER KEY HERE';
$host = "https://api.cognitive.microsofttranslator.com";
$path = "/translate?api-version=3.0";
// Translate to German and Italian.
$params = "&to=de&to=it";
$text = "Hello, world!";
if (!function_exists('com_create_guid')) {
function com_create_guid() {
return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
mt_rand( 0, 0xffff ),
mt_rand( 0, 0x0fff ) | 0x4000,
mt_rand( 0, 0x3fff ) | 0x8000,
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
);
}
}
function Translate ($host, $path, $key, $params, $content) {
$headers = "Content-type: application/json\r\n" .
"Content-length: " . strlen($content) . "\r\n" .
"Ocp-Apim-Subscription-Key: $key\r\n" .
"X-ClientTraceId: " . com_create_guid() . "\r\n";
// NOTE: Use the key 'http' even if you are making an HTTPS request. See:
// http://php.net/manual/en/function.stream-context-create.php
$options = array (
'http' => array (
'header' => $headers,
'method' => 'POST',
'content' => $content
)
);
$context = stream_context_create ($options);
$result = file_get_contents ($host . $path . $params, false, $context);
return $result;
}
$requestBody = array (
array (
'Text' => $text,
),
);
$content = json_encode($requestBody);
$result = Translate ($host, $path, $key, $params, $content);
// Note: We convert result, which is JSON, to and from an object so we can pretty-print it.
// We want to avoid escaping any Unicode characters that result contains. See:
// http://php.net/manual/en/function.json-encode.php
$json = json_encode(json_decode($result), JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
echo $json;
I also have a Google Cloud account and am looking for something similar for a few languages that Google supports that BING does not. For v2, it is not too hard to call Google to make the return the translations.
I found this GitHub project that seems to work for v2 API calls with an API key, but unfortunately I think that is a fee-for-service program now ?
google-cloud-php-translate
That also seems to work pretty well if you have an API key. If you are using v3, they apparently updated the libraries and support. You can make a CURL call from the command line and they have some of that documented on their website, but I am looking for a way to make the call using a PHP file.
require 'vendor/autoload.php';
use Google\Cloud\Translate\TranslateClient;
$translate = new TranslateClient([
'key' => 'APIKEY'
]);
// Translate text from english to french.
$result = $translate->translate('Hello world!', [
'target' => 'fr'
]);
echo $result['text'] . "\n";
// Detect the language of a string.
$result = $translate->detectLanguage('Greetings from Michigan!');
echo $result['languageCode'] . "\n";
// Get the languages supported for translation specifically for your target language.
$languages = $translate->localizedLanguages([
'target' => 'en'
]);
foreach ($languages as $language) {
echo $language['name'] . "\n";
echo $language['code'] . "\n";
}
// Get all languages supported for translation.
$languages = $translate->languages();
foreach ($languages as $language) {
echo $language . "\n";
}
Not sure that is even possible, but the best I can come up with is something like this based upon the command line CURL, but the authentication is wrong and fails. I do have the .json file for my Project/Service credentials. The ${PROJECT_ID} I presume is the project ID for the account, and Bearer $(gcloud auth application-default print-access-token) I am not sure about. There are some instructions about how to obtain that through the CLI, but is there a way to get that via a PHP file ? Like I say, the v2 version works fine.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://translation.googleapis.com/v3beta1/projects/${PROJECT_ID}/locations/global:detectLanguage");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\n mimeType: 'text/plain',\n content: 'Omnia Gallia est divisa in tres partes'\n}");
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = 'Authorization: Bearer $(gcloud auth application-default print-access-token)';
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
} else {
echo $result;
}
curl_close ($ch);
There might be clues here, but it talks about exporting the path for the credentials file and running PHP scripts from the command line instead of from a server.
Google Cloud Translate API Samples
I have an irrational dislike for using client libraries, so I didn't install the Google PHP library. From what I can tell, the only way to get the authentication to work is to actually go through the whole Oauth2 process. I think the PHP library handles some of that for you, but this code should work as a standalone solution.
First, make sure you've got your Google Cloud Platform account set up, then create a project, then enable the Translation API, after that, create and configure an API key before creating and configuring an OAuth 2.0 client (make sure you enter the correct redirect url). Nothin' to it! ;-)
If you succeed in getting all of that squared away, you should be good to go!
The page effectively redirects the user to the same page they were just on, but includes the results of a GET request in the url. The response contains a code, that can be used to make another GET request in order to retrieve the access token, and once you've got the access token, you can make a POST request to perform the actual translation.
<?php
$clientId = "{your client id}";
$clientSecret = "{your client secret}";
$clientRedirectURL = "{your redirect URL}";
$login_url = 'https://accounts.google.com/o/oauth2/v2/auth?scope=' . urlencode('https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/cloud-translation') . '&redirect_uri=' . urlencode($clientRedirectURL) . '&response_type=code&client_id=' . $clientId . '&access_type=online';
if (!isset($_GET['code'])){
header("location: $login_url");
} else {
$code = filter_var($_GET['code'], FILTER_SANITIZE_STRING);
$curlGet = '?client_id=' . $clientId . '&redirect_uri=' . $clientRedirectURL . '&client_secret=' . $clientSecret . '&code='. $code . '&grant_type=authorization_code';
$url = 'https://www.googleapis.com/oauth2/v4/token' . $curlGet;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$data = curl_exec($ch);
$data = json_decode($data, true);
curl_close($ch);
$accessToken = $data['access_token'];
$apiKey = "{your api key}";
$projectID = "{your project id}";
$target = "https://translation.googleapis.com/v3/projects/$projectID:translateText?key=$apiKey";
$headers = array(
"Content-Type: application/json; charset=utf-8",
"Authorization: Bearer " . $accessToken,
"x-goog-encode-response-if-executable: base64",
"Accept-language: en-US,en;q=0.9,es;q=0.8"
);
$requestBody = array();
$requestBody['sourceLanguageCode'] = "en";
$requestBody['targetLanguageCode'] = "pt";
$requestBody['contents'] = array("So, I guess this thing works?");
$requestBody['mimeType'] = "text/plain";
$ch = curl_init($target);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($requestBody));
$data = curl_exec($ch);
curl_close($ch);
echo $data;
}
Also, I found this tutorial quite helpful.
This probably won't evaluate:
'Authorization: Bearer $(gcloud auth application-default print-access-token)'
It's rather:
// $cmd = 'gcloud auth application-default login';
$cmd = 'gcloud auth application-default print-access-token';
$token = shell_exec($cmd);
besides it probably should be a service account.
It seems google/cloud replaces google/cloud-translate. For Translate, you could edit the translate-v2.json or add translate-v3beta1.json; but v3beta1 has a whole other REST API than v2 ...
I am using the following code and am not able to display amazon.com using php and curl. Im using curl_error and getting no errors, so I'm not sure what im doing wrong
<?php
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'https://www.amazon.com');
curl_exec($curl);
curl_close ($curl);
I'm doing this on local host
just display amazon then use this
echo file_get_contents("https://www.amazon.com");
You should use the following:
$response = curl_exec($curl);
$result is an array. You can get for example the body of the request by using:
$header_size = curl_getinfo($curl,CURLINFO_HEADER_SIZE);
$result['header'] = substr($response, 0, $header_size);
$result['body'] = substr( $response, $header_size );
$result['http_code'] = curl_getinfo($curl,CURLINFO_HTTP_CODE);
$result['last_url'] = curl_getinfo($curl,CURLINFO_EFFECTIVE_URL);
echo $result['body'];
For more information: http://php.net/manual/de/function.curl-exec.php
when debugging curl code, use CURLOPT_VERBOSE, and post the CURLOPT_VERBOSE log when asking for help. also when debugging, do not ignore the return values of curl_setopt, because it returns bool(false) if there was an error, and if there was an error, that error would probably explain why the code isn't working. also do not ignore the return value of curl_exec, because it returns bool(false) if there was an error, which goes unnoticed if you ignore the return value (and your code does)
here is a version of your code that doesn't ignore any errors and enable CURLOPT_VERBOSE logging, it should reveal where your code fails:
<?php
$curl = curl_init();
if (! is_resource($curl)) {
throw new \RuntimeException('curl_init() failed!');
}
ecurl_setopt($curl, CURLOPT_URL, 'https://www.amazon.com');
ecurl_setopt($curl, CURLOPT_VERBOSE, 1);
$curlstderr = etmpfile();
$curlstdout = etmpfile();
ecurl_setopt($curl, CURLOPT_STDERR, $curlstderr);
ecurl_setopt($curl, CURLOPT_FILE, $curlstdout);
if (true !== curl_exec($curl)) {
throw new \RuntimeException("curl_exec failed! " . curl_errno($curl) . ": " . curl_error($curl));
}
rewind($curlstderr); // https://bugs.php.net/bug.php?id=76268
rewind($curlstdout); // https://bugs.php.net/bug.php?id=76268
$verbose = stream_get_contents($curlstderr);
$output = stream_get_contents($curlstdout);
curl_close($curl);
fclose($curlstderr);
fclose($curlstdout);
var_dump($verbose, $output);
function ecurl_setopt ( /*resource*/$ch, int $option , /*mixed*/ $value): bool
{
$ret = curl_setopt($ch, $option, $value);
if ($ret !== true) {
// option should be obvious by stack trace
throw new RuntimeException('curl_setopt() failed. curl_errno: ' . return_var_dump(curl_errno($ch)) . '. curl_error: ' . curl_error($ch));
}
return true;
}
function etmpfile()
{
$ret = tmpfile();
if (false === $ret) {
throw new \RuntimeException('tmpfile() failed!');
}
return $ret;
}
also, it appears that https://www.amazon.com has a bug, see is it a bug to send response gzip-compressed to clients that doesn't specify Accept-Encoding: gzip?
in any case, to make curl automatically decompress the gzip-compressed response from amazon, add ecurl_setopt($curl,CURLOPT_ENCODING,''); , that tells libcurl to add the Accept-Encoding: gzip,deflate header, and automatically decompress the result.
I use CURL in php, and I use CURL something like this
$url = "http://exampledomain.com";
$smsURL = $url;
$curl = curl_init();
curl_setopt ($curl, CURLOPT_URL, $smsURL);
curl_exec ($curl);
curl_close ($curl);
This is not working, but if I wrote "http://exampledomain.com" in place of "$smsURL" at curl_setopt (); It will work fine. Where is issue in my code? did I miss something?
Original Code
$url = $this->conf['sms_getway_url'];
$url .= '&recipient=' . $_POST['txt_customer_contact_no'];
$url .= '&sender=' . strtoupper($saloon_info['saloon_name']);
$url .= '&is_payor=' . $this->conf['sms_is_payor'];
$url .= '&pay_amount=' . $this->conf['sms_pay_amount'];
$url .= '&token=5ce7467e9ec045cbbac448ba5a422a02';
//$url .= '&customer_num=' . $this->conf['sms_customer_num'] . $saloon_id;
$url .= '&customer_num=' . $this->conf['sms_customer_num'];
$appointment_time = date('H:i', strtotime($app_start_time));
$employee_name = $_POST['hdn_selected_employee_name']; //$value['id_employee'];
//$sms_msg = "Hey. Recalling that I await tomorrow at. " . $appointment_time . " Regards " . $employee_name . ", " . $saloon_name . ". ";
$sms_msg = t('msg_sms_book_appointment', array('%emp_name' => $employee_name, '%saloon_name' => $_POST['hdn_selected_saloon_name'], '%time' => $appointment_time));
$url .= '&sms_msg=' . $sms_msg;
$smsURL = $url;
$curl = curl_init();
curl_setopt ($curl, CURLOPT_URL, $smsURL);
curl_exec ($curl);
curl_close ($curl);
Thanks
You compose the URL from pieces but you don't encode the values properly. There are characters that have special meaning in URLs (/, ?, &, =, %, , + and a few more). They have to be encoded when they appear in the values from the query string, in order to retain their literal meaning.
PHP helps you for this goal with function urlencode() that can be used to encode each value individually when you create a query string. Something like this:
$url = $this->conf['sms_getway_url'];
$url .= '&recipient=' . urlencode($_POST['txt_customer_contact_no']);
$url .= '&sender=' . urlencode(strtoupper($saloon_info['saloon_name']));
...
But, because this is a tedious work, it also provides an easier method. Put all the values you need into an array, using the names of the variables as keys, then pass the array to function http_build_query(). There is no need to call urlencode() any more; http_build_query() takes care of it. Also it puts ampersands (&) between the variables and equals (=) where they belong.
The code is like this:
$url = $this->conf['sms_getway_url'];
// Prepare the values to put into the query string
$vars = array();
$vars['recipient'] = $_POST['txt_customer_contact_no'];
$vars['sender'] = strtoupper($saloon_info['saloon_name']);
$vars['is_payor'] = $this->conf['sms_is_payor'];
$vars['pay_amount'] = $this->conf['sms_pay_amount'];
$vars['token'] = '5ce7467e9ec045cbbac448ba5a422a02';
$vars['customer_num'] = $this->conf['sms_customer_num'];
$appointment_time = date('H:i', strtotime($app_start_time));
$employee_name = $_POST['hdn_selected_employee_name'];
$sms_msg = t('msg_sms_book_appointment', array(
'%emp_name' => $employee_name,
'%saloon_name' => $_POST['hdn_selected_saloon_name'],
'%time' => $appointment_time,
));
$vars['sms_msg'] = $sms_msg;
// Now, the magic comes into place
$smsURL = $url.'?'.http_build_query($vars);
$curl = curl_init();
curl_setopt ($curl, CURLOPT_URL, $smsURL);
if (! curl_exec ($curl)) {
// Something went wrong. Check the status code (at least)
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
// Do something here.
// If $code >= 500 then the remote server encountered an internal error
// retry later or ask them to fix it
// If 400 <= $code < 500 then there is a problem with the request:
// maybe the resource is not there (404, 410)
// or you are not allowed to access it (403)
// or something else.
echo('Failure sending the SMS. HTTP status code is '.$code."\n");
}
curl_close ($curl);
Check the list of HTTP status codes for more details.
I'm creating an application for Google App engine, where CURL isn't allowed.
As far as I know, urlFetch is the best alternative.
I don't know if I can achieve the same result with urlFetch, but I would really, really appreciate it if anyone with more experience could help me out.
The plan was to convert the following CURL requests to urlFetch. If anyone can point me in the right direction, or propose a better alternative, I'd greatly appreciate it.
public function postCall($endpoint, $post_data, $param1, $param2, $json=1, $headers=false) {
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $this->options['url'].$endpoint);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
if ($headers && is_array($headers)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}
$post_data['req_token'] = $this->hash($param1, $param2);
curl_setopt($ch, CURLOPT_POST, count($post_data));
if (!$headers)
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));
else
curl_setopt($ch,CURLOPT_POSTFIELDS, $post_data);
$this->debug('POST params: ' . json_encode($post_data));
$result = curl_exec($ch);
if ($result === false) {
$this->debug('CURL error: '.curl_error($ch));
return false;
}
$this->debug('HTTP response code' . curl_getinfo($ch, CURLINFO_HTTP_CODE));
$this->debug('POST return ' . $result);
// close connection
curl_close($ch);
if ($json)
return json_decode(utf8_encode($result), true);
else
return $result;}
Did you look at the Urlfetch documentation and the linked PHP article about wrappers?. You can experiment with this live shell.
The code could be translated to something like:
public function postCall($endpoint, $post_data, $param1, $param2, $json=1, $headers=false) {
$post_data['req_token'] = $this->hash($param1, $param2);
$this->debug('POST params: ' . json_encode($post_data));
$data = http_build_query($post_data);
$options =
array("http"=>
array(
"method" => "POST",
"content" => $post_data,
)
);
if ($headers && is_array($headers)) {
$options["http"]["header"] = $headers;
}
$context = stream_context_create($options);
$result = file_get_contents("http://app.com/path?query=update", false, $context);
if ($result === FALSE) {
$this->debug('Error: '. print_r($http_response_header));
return FALSE;
}
$this->debug('Response headers:' . print_r($http_response_header)); // To get the status code you would need to parse that response
$this->debug('POST return ' . $result);
if ($json)
return json_decode(utf8_encode($result), true);
else
return $result;
}
Here is simple library which replaces curl native functions with urlfetch. https://github.com/azayarni/purl
Someone here suggested using the PURL from azayarni. Let me warn you: avoid using it on Google App Engine. I spent SEVERAL days trying to get it work without success: the Google PHP Client SDK is rewriting itself some CURL functions and simply PURL mess it up a lot. Some things were working, some were not. The URLFETCH tool is much more easier and safe.
Its a very old post, but just an update: Google App Engine now supports cURL with its PHP 5.5 runtime.