I want to make a POST call from PHP to my python API
here is the equivalent in Python:
import requests
import json
payload = json.dumps({'serviceID':'131001086184'})
r = requests.post('http://17.../todo/api/line/v1.0/tasks', data=dict(data=payload))
print r.content
Works like a charm.
Here the equivalent in curl:
curl -i -F data='{"serviceID":"131001086184"}' http://17.../todo/api/line/v1.0/tasks
Works too.
Now PHP:
<?php
$url = 'http://17.../todo/api/line/v1.0/tasks';
$postData = array('serviceID' => '131001086184');
$context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => "Content-Type: application/json\r\n"
'content' => json_encode($postData)
)
));
$response = file_get_contents($url. FALSE. &context)
$responseData = json_decode($response, TRUE);
echo $responseData['published'];
?>
Does not work and I dont get error message.
What am I missing?
In your code $response = file_get_contents($url. FALSE. &context)
You are concatenating $url with FALSE and &context,
Use the following:-
$response = file_get_contents($url, FALSE, &context);
Related
I want to pass parameters with cURL or file_get_contents and get it using argv
Using cURL
curl -X POST -H "Content-Type: application/json" -d '{"param1": "MY DATA"}' 'http://[serverIP]/file.php'
using file_get_contents
<?php
$url = 'http://[serverIP]/file.php';
$data = array('param1' => 'MY DATA');
$options = array('http' => array(
'method' => 'POST',
'content' => http_build_query($data)
));
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
print_r($result);
and I want this data as an argument - like it should print an array of arguments like
Array
(
[0] => directory/file.php
[1] => MY DATA
)
Please Note that I can use POST/GET method for getting the data but I want to know can I use argv when using curl command or anything else similar?
Sorry for the messy code in advance. I want to write a code which returns me infos from the official Blizzard API, which I can then print out on my homepage. The code doesn't throw any errors but it doesn't print out something either. For Starters:
I would also prefer using CURL, but my homepage is on a Wordpress Hosting Site and I don't know how to install the CURL Library that way
allow_furl_open is on
$url = "https://eu.battle.net/oauth/token";
$data = array('grant_type' => 'client_credentials');
//HTTP options
$opts = array('http' =>
array(
'method' => 'POST',
'header' => array ('Content-type: multipart/form-data', 'Authorization: Basic ' .
base64_encode("$client_id:$client_pass")),
'content' => json_encode($data)
)
);
//Do request
$context = stream_context_create($opts);
$json = file_get_contents($url, false, $context);
$result = json_decode($json, true);
$accessToken = $json['access_token'];
$tokenURL = "https://us.api.blizzard.com/data/wow/token/?namespace=dynamic-eu";
$opts2 = array('http' =>
array(
'method' => 'POST',
'header' => array('Content-type: multipart/form-data', 'Authorization: Bearer ' . $accessToken),
)
);
$context2 = stream_context_create($opts2);
$json2 = file_get_contents($tokenURL,false,$context2);
$result2 = json_decode($json2, true);
$tokenprice = $result2['price'];
echo "<p>Tokenpreis:" .$tokenprice. "</p>";
I didn't add the $client_id and $client_pass into the code snippet, but this exists obviously. I used this PHP CURL Snippet as template. And this is a short explanation on blizzard's site on how is this supposed to work:
Anyone got any ideas what went wrong? I am really out of ideas here and would love anyone who could help.
Thanks in advance
Based on the curl example from the linked API docs, the content type should be application/x-www-form-urlencoded, and so for the initial request to https://eu.battle.net/oauth/token you should be able to follow the example here.
In your case this will look something like:
$url = 'https://eu.battle.net/oauth/token';
$data = array('grant_type' => 'client_credentials');
$opts = array(
'http' => array(
'method' => 'POST',
'header' => array (
'Content-type: application/x-www-form-urlencoded',
'Authorization: Basic ' . base64_encode("$client_id:$client_pass")
),
'content' => http_build_query($data)
)
);
$context = stream_context_create($opts);
$json = file_get_contents($url, false, $context);
Also, in your code you are storing the raw result from the initial request in $json, then the decoded array in $result, but attempting to get the access_token from the initial request, instead of the decoded array.
I'm testing an api using Postman but it doesnt send any data.
When I create a test using php function it works fine.
the api php
$json_str = file_get_contents("php://input");
$obj = json_decode($json_str, false);
the test php
$obj->CompanyCode= "39170";
$obj->CustomerNumber= "1800001";
$options = array(
'http' => array(
'method' => 'POST',
'content' => json_encode( $obj ),
'header'=>array(
"Content-Type: application/json",
"Accept: application/json")
)
);
$context = stream_context_create( $options );
$result = file_get_contents( "https://www.url.com/inquiry/", false, $context );
the postman
postman header
postman body
I have to send a GET Request within a PHP Script to get data from an API with a certain given client-id without the usage of cURL.
In the terminal I use following command:
curl -i -u “123456-123ea-123-123-123456789:“-H “Content-Type:
application/json” -X GET
“https://api.aHoster.io/api/rest/jobboards/jobs”
(123456-123ea-123-123-123456789 in this command is the given client-id I got)
Everything works fine in the terminal, but now I like to do this GET request in a php script.
How do I add the client-id in this attempt?
<?php
$url = 'https://api.aHoster.io/api/rest/jobboards/jobs';
$client_id = '123456-123ea-123-123-123456789';
$options = array(
'http' => array(
'header' => "Content-type: application/json\r\n",
'method' => 'GET',
)
);
$context = stream_context_create($options);
$result = #file_get_contents($url, false, $context);
if ($http_response_header[0] == "HTTP/1.1 200 OK") {
//...
} else {
// ...
}
?>
Based on your cURL command you want to add an Authorization header:
$options = array(
'http' => array(
'header' => "Content-type: application/json\r\n" .
"Authorization: Basic " . base64_encode($client_id) . "\r\n",
'method' => 'GET',
)
);
I'm not sure but you may need a colon showing empty password "$client_id:".
Also, do a print_r($http_response_header); afterwards as it may contain information such as "Authorization Required" or something else useful.
I'm trying to send a special character, such as ñ, through a POST request in PHP. When I do it, it comes out as ñ, what is wrong and how do I fix it?
I'm sending and receiving the post request in PHP, here is what I use to send it:
$url = '<url>';
$data = array('key' => 'ñ');
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
echo $result;
Thanks in advance!
Try using this:
$data = array('key' => urlencode('ñ'));
And in the $url file:
$_POST['key']=urldecode($_POST['key']);
This is how I use to send special characters in GET and POST method with ajax, it must work for php too.