I want send a get request with file_get_contents. This code returns failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found.
How can I solve it?
<?php
define('API_KEY','eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...');
$data = http_build_query(
array(
'name' => 'test',
'count' => '1000',
)
);
$url = 'https://www.example.com/order';
$options = array('http' => array(
'method' => 'GET',
'content' => $data,
'header' =>
"Content-Type: application/x-www-form-urlencoded\r\n".
"Authorization: Bearer ".API_KEY
)
);
$context = stream_context_create($options);
$response1 = file_get_contents($url, false, $context);
print_r($response1) ;
Also I tried this bottom code, but it returns the error again!
$url = 'https://www.example.com/order?name=test&count=1000';
$options = array('http' => array(
'method' => 'GET',
'header' =>
"Content-Type: application/x-www-form-urlencoded\r\n".
"Authorization: Bearer ".API_KEY
)
);
Related
I have this php scripts:
$url = "https://mysite/web/session/authenticate";
$data = array (
'params' =>
array (
'db' => 'mydb',
'login' => 'myuserid',
'password' => 'myuserpwd',
),
);
$data = json_encode($data);
$options = array(
'http' => array(
'header' => "Content-Type: application/json\r\n"
. "Content-Length: " . strlen($data) . "\r\n",
'method' => 'POST',
'content' => $data,
)
);
$context = stream_context_create($options);
$response = file_get_contents($url, false, $context);
From the server response I need to retrieve a key-value pair (set-cookie) contained in the headers.
Response fron POSTMAN
How can I do?
Thanks
if I use file_get_contents for sending post data to another .php with like the example given by the manual further down, could a visitor of this webpage have any possiblity to view/trace the post data that i send? Thanks
<?php
$postdata = http_build_query(
array(
'var1' => 'some content',
'var2' => 'doh'
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = file_get_contents('http://example.com/submit.php', false, $context);
?>
https://www.php.net/manual/en/context.http.php
While trying to fetch the active subscriber count from Sendy API, I am getting following error
The POST request looks like this:
http://my-sandy-Installation/api/subscribers/active-subscriber-count.php
In header: Content-Type:application/x-www-form-urlencoded
In Body:
api_key= mykey
list_id= mylistid
Can anyone please help?
$boolean = 'true';
$postdata = http_build_query(
array(
'email' => $email,
'api_key'=>'your api_key',
'list' => 'Your List id',
'boolean' => 'true'
)
);
$opts = array('http' => array('method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => $postdata));
$context = stream_context_create($opts);
$result = file_get_contents($your_installation_url.'/subscribe', false, $context);
here is my code:
$url = 'https://api.linkedin.com/v1/people/~/shares?oauth2_access_token=XXXXXXX';
$url .= '&format=json';
$postdata = http_build_query(
array(
'comment' => 'comment',
'content' => array('title' => 'LInkedIN title',
'description' => 'linkedin desc',
'submitted-url' => "https://developer.linkedin.com",
),
'visibility' => array('code' => 'anyone')
)
);
$opts = array('http' =>
array('method' => 'POST',
'header'=> "x-li-format : json\r\n".
"Host: api.linkedin.com\r\n".
"Content-Length: ".strlen($postdata)."\r\n".
"X-Target-URI: https://api.linkedin.com\r\n".
"Content-Type: application/json\r\n".
"Connection: Keep-Alive",
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = file_get_contents($url, false, $context);
but there is no effect. Tried this service https://apigee.com/console/linkedin - works perfectly...
What's wrong?
what are you trying to build? the jssdk can do this much better! Check out this tool I developed:
http://datopstech.com/linkedin-share-tool/
and the source code:
https://github.com/kaburkett/LinkedIn-Advanced-Share
And base your project off that if you can.
Is it possible to make file_get_contents show me the actual response even if an error occurs?
It is difficult to debug otherwise. For instance, say you have the following code:
$url = 'https://api.twitter.com/oauth/request_token';
$data = array();
$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);
var_dump($result);
var_dump($http_response_header);
This shows me NULL for the result and the HTTP headers, but I want to get the actual message Twitter sends back (should be something like Failed to validate oauth signature and token), which is what you get if you try to load https://api.twitter.com/oauth/request_token in your browser.
There is a very simple switch for the context. just add this line to options:
'ignore_errors' => true
so you will get
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
'ignore_errors' => true
)
);