I have this code with which I am trying to consume a web service from WordPress page.php.
if I use $urlOdata,$username,$password I can consume the web service in the browser.
But on the web page it returns: "12bool(false) 3resource(1841) of type (stream-context) Resource id #1841"
<?php
$urlOdata = 'enlace';
$username = 'usuario';
$password = 'pass';
try {
$call_opts = array(
'http' => array(
'method' => 'GET',
'header' => 'Authorization: Basic ' . base64_encode( $username . ':' . $password ) . "\r\n" . 'Content-Type: application/json',
),
);
$call_context = stream_context_create( $call_opts );
$call_res_json = file_get_contents( $urlOdata, false );
print( '1' );
print( $call_res_json );
print( '2' );
var_dump( $call_res_json );
print( '3' );
var_dump( $call_context );
echo $call_context;
} catch ( Exception $e ) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
?>
I would like to receive and display the JSON that the web service sends.
Related
I've been working with this api and can't get anything response. Apparently the response should be true or false.
It returns Array;
I've tried things like:
return $obj[0][0];
But the page is blank
Here's the code:
function checkGuest ( $intEvent, $strBarcode, $ourSubscriptionKey, $sessionTokenClient )
{
$body = '';
$opts = array('http' =>
array(
'method' => 'GET',
'header' =>
"Content-Type: application/json\r\n".
"Authorization: Bearer " . $sessionTokenClient ."\r\n".
"Ocp-Apim-Subscription-Key: " . $ourSubscriptionKey,
'content' => $body,
'timeout' => 60
) );
$context = stream_context_create($opts);
$url = 'https://api.qflowhub.io/v1/api/guests/'.$intEvent.'/bybarcode?barcode='.$strBarcode;
try
{
$result = file_get_contents($url, false, $context);
$obj = json_decode( $result );
return $obj;
}
catch (Exception $ex)
{
$result = $ex;
}
}
$userCheck = checkGuest ( $intEvent, $strBarcode, $ourSubscriptionKey, $sessionTokenClient ) ;
echo $userCheck;
I am trying to submit a new subscriber via a form on a WordPress website. I am concentrating on sending the subscriber information to a list in MailChimp. I am getting the following error:
string(75) "cURL error 6: Could not resolve host: us19.api.mailchimp.com; Unknown error"
My question is whether there is an issue with the arguments being passed to the wp_remote_post() function?
This is my code:
<?php
$api_key = '[HIDDEN]';
$list_id = '[HIDDEN]';
$email = "[HIDDEN]";
$firstname = "[HIDDEN]";
$lastname = "[HIDDEN]";
$status = 'subscribed'; // subscribed, cleaned, pending
$args = array(
'method' => 'PUT',
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( 'user:'. $api_key )
),
'body' => json_encode(array(
'email_address' => $email,
'status' => $status
))
);
$response = wp_remote_post( 'https://' . substr($api_key,strpos($api_key,'-')+1) . '.api.mailchimp.com/3.0/lists/' . $list_id . '/members/' . md5(strtolower($email)), $args );
var_dump($response);
?>
I am trying to use file_get_contents to "post" to a url and get auth token. The problem I am having is that it returns a error.
Message: file_get_contents(something): failed to open stream: HTTP request failed! HTTP/1.1 411 Length Required.
I am not sure what is causing this but need help. Here is the function.
public function ForToken(){
$username = 'gettoken';
$password = 'something';
$url = 'https://something.com';
$context = stream_context_create(array (
'http' => array (
'header' => 'Authorization: Basic ' . base64_encode("$username:$password"),
'method' => 'POST'
)
));
$token = file_get_contents($url, false, $context);
if(token){
var_dump($token);
}else{
return $resultArray['result'] = 'getting token failed';
}
}
I tried it with POSTMAN, and it works, so only problem I am having is that why isnt it working with file_get_contents.
Cannot command so i will do it this way. If you use Postman why don't you let Postman generate the code for you. In postman you can click code at a request and you can even select in what coding language you wanne have it. Like PHP with cURL:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://somthing.com/",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => array(
"password: somthing",
"username: gettoken"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
Hope this will help!
I can not comment because of my reputation;
In some servers file_get_content is not available...
You may try with CURL like this
Your request havent any data and content length is missing.
You can try this:
public function ForToken(){
$username = 'gettoken';
$password = 'something';
$url = 'https://something.com';
$data = array('foo' => 'some data');
$data = http_build_query($data);
$context_options = array (
'http' => array (
'method' => 'POST',
'header'=> "Content-type: application/x-www-form-urlencoded\r\n"
. "Authorization: Basic " . base64_encode("$username:$password") . "\r\n"
. "Content-Length: " . strlen($data) . "\r\n",
'content' => $data
)
);
$context = context_create_stream($context_options);
$token = file_get_contents($url, false, $context);
if(token){
var_dump($token);
}else{
return $resultArray['result'] = 'getting token failed';
}
}
Here is similar problem:
How to fix 411 Length Required error with file_get_contents and the expedia XML API?
RFC2616 10.4.12
https://www.rfc-editor.org/rfc/rfc2616#section-10.4.12
Code: (Doesn't work, see below)
public function ForToken(){
$username = 'gettoken';
$password = 'something';
$url = 'https://something.com';
$context = stream_context_create(array (
'http' => array (
'header' => 'Authorization: Basic ' . base64_encode("$username:$password") . 'Content-Length: ' . strlen($url) . '\r\n',
'method' => 'POST'
)
));
$token = file_get_contents($url, false, $context);
if(token){
var_dump($token);
}else{
return $resultArray['result'] = 'getting token failed';
}
}
2nd Try:
public function ForToken(){
$username = 'gettoken';
$password = 'something';
$url = 'https://something.com';
$context_options = array (
'http' => array (
'method' => 'POST',
'header'=> "Content-type: application/x-www-form-urlencoded\r\n" . "Authorization: Basic " . base64_encode("$username:$password") . "Content-Length: " . strlen($url) . "\r\n",
'content' => $url
)
);
$context = stream_context_create($context_options);
$token = file_get_contents($url, false, $context);
if(token){
var_dump($token);
}else{
return $resultArray['result'] = 'getting token failed';
}
}
This is from patryk-uszynski's answer formatted with the OP's code.
I'm very new to programming and using API. I am currently working on a project to take data from BLS.GOV using their API to port the data to my website! so far, BLS has given me this code:
$url = 'http://api.bls.gov/publicAPI/v2/timeseries/data/';
$method = 'POST';
$query = array(
'seriesid' => array('LEU0254555900', 'APU0000701111'),
'startyear' => '2002',
'endyear' => '2012'
);
$pd = json_encode($query);
$contentType = 'Content-Type: application/json';
$contentLength = 'Content-Length: ' . strlen($pd);
$result = file_get_contents(
$url, null, stream_context_create(
array(
'http' => array(
'method' => $method,
'header' => $contentType . "\r\n" . $contentLength . "\r\n",
'content' => $pd
),
)
)
);
var_dump($http_response_header);
var_dump($result);
How would I be able to grab graph data with PHP from this link.
One site requires login for three variables: username, password and token. This data is sent via POST.
token is also sent through a section.
I have all three variables, and I want to log in via file_get_contents.
How can that data be sent and the authentication is successful?
function file_post_contents()
{
$url = 'http://site.com/auth.php?' . session_name() . '=' . session_id();
$login = 'jhon';
$senha = 'doe';
$token = '123456';
$_SESSION["token"] = $token;
$postdata = http_build_query(
array(
'login' => $login,
'senha' => $senha,
'token' => $token,
$_SESSION["token"] => $token
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
if($login && $senha)
{
$opts['http']['header'] = ("Authorization: Basic " . base64_encode("$login:$senha"));
}
$context = stream_context_create($opts);
return file_get_contents($url, false, $context);
}
Modified code. It does not work.
function file_post_contents()
{
$url = 'http://site.com/auth.php?' . session_name() . '=' . session_id();
$login = 'jhon';
$senha = 'doe';
$token = '123456';
$_SESSION["token"] = $token;
$postdata = http_build_query(
array(
'login' => $login,
'senha' => $senha,
'token' => $token
//, $_SESSION["token"] => $token
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded'
. "\r\n" . "Authorization: Basic " . base64_encode("$login:$senha"). "\r\n",
'content' => $postdata
)
);
$context = stream_context_create($opts);
return file_get_contents($url, false, $context);
}
Your headers might be set wrong, try adding \r\n like so
$opts = array('http' =>
array(
'method' => 'POST',
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'content' => $postdata
)
);
and later down the code
$opts['http']['header'] = $opts['http']['header'] . "Authorization: Basic " . base64_encode("$login:$senha") . "\r\n";
You're overriding $opts['http']['header'] with the Basic auth. You're not adding it to the Content-type, you're replacing Content-type with it.