Why does curl turn null post arguments into empty strings? - php

When making a curl post to another script, sometimes I may want to send null as the value for certain keys:
$postdata = array(
'userID' => 0,
'questionText' => "Can you answer this question?",
'time' => null);
$req = curl_init($url);
curl_setopt($req, CURLOPT_POST, true);
curl_setopt($req, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($req, CURLOPT_RETURNTRANSFER, false);
curl_setopt($req, CURLOPT_VERBOSE, true);
$data = curl_exec($req);
curl_close();
However, the data seems to have changed by the time it is received. Doing a vardump on $_POST reveals that the 'time' value is now an empty string:
array(3) {
["userID"]=>
string(1) "0"
["questionText"]=>
string(29) "Can you answer this question?"
["time"]=>
string(0) ""
}
What's going on here? Why is everything a string? How can I perserve the typing?

As comments to the original question state, HTTP requests will only ever provide strings.

Related

How to retrieve certain data in string?

Hello may i know how to retrieve data in string?
$url = 'test url';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$info = curl_getinfo($ch);
$response = curl_exec($ch);
curl_close ($ch);
echo 'Response: ';
echo gettype($response);
echo '<br>';
echo($response);
Output :
Response: string
TRANSACTION_ID=abc123
MERCHANT_ACC_NO=M213213
TXN_STATUS=A
TRAN_DATE=2020-07-20
CAPTURE_DATE=2020-07-20
SALES_DATE=2020-07-20
RESPONSE_CODE=1
RESPONSE_MESSAGE=Success
As you can see the output of the code is as shown above. This is my first time to encounter this kind of output because usually I get json as the output. So my question is may I know how to retrieve the RESPONSE_MESSAGE in the output or may I know how to convert the output to array or json so that I can easily retrieve the data. Sorry for asking this I'm quite new with this PHP and CURL function.
You can explode to lines and explode the lines to parts in a foreach.
Edit: I realized the "Response:" was actually outputted manually.
Changed the code to slice the array from second item instead.
foreach(array_slice(explode("\n", $str), 1) as $line){
$temp = explode("=", $line);
$res[$temp[0]] = $temp[1];
}
Output:
array(8) {
["TRANSACTION_ID"]=>
string(6) "abc123"
["MERCHANT_ACC_NO"]=>
string(7) "M213213"
["TXN_STATUS"]=>
string(1) "A"
["TRAN_DATE"]=>
string(10) "2020-07-20"
["CAPTURE_DATE"]=>
string(10) "2020-07-20"
["SALES_DATE"]=>
string(11) "2020-07-20 "
["RESPONSE_CODE"]=>
string(1) "1"
["RESPONSE_MESSAGE"]=>
string(7) "Success"
}
https://3v4l.org/evdMO

Unable to integrate Azure ML API with PHP

I tried integrating Azure ML API with PHP but unfortunately getting an error in response.
Updated: I have used request response API sending through json response
Below is the response obtained on executing PHP script:
array(1) { ["error"]=> array(3) { ["code"]=> string(11) "BadArgument"
["message"]=> string(26) "Invalid argument provided." ["details"]=> array(1)
{[0]=> array(2) { ["code"]=> string(18) "RequestBodyInvalid" ["message"]=>
string(68) "No request body provided or error in deserializing the request
body." } } } }
PHP Script:
$url = 'URL';
$api_key = 'API';
$data = array(
'Inputs'=> array(
'My Experiment Name'=> array(
"ColumnNames" => [['Column1'],
['Column2'],
['Column3'],
['Column4'],
['Column5'],
['Column6'],
['Column7']],
"Values" => [ ['Value1'],
['Value2'],
['Value3'],
['Value4'],
['Value5'],
['Value6'],
['Value7']]
),
),
'GlobalParameters' => new StdClass(),
);
$body = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Bearer '.$api_key, 'Accept: application/json'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = json_decode(curl_exec($ch), true);
//echo 'Curl error: ' . curl_error($ch);
curl_close($ch);
var_dump ($response);
I have followed few examples, still unable to crack it. Please let me know the solution for this.
According to the error information, I think the issue was caused by requesting the ML REST API without correct json body.
I suggest that you can refer to the article "Getting started with the Text Analytics APIs to detect sentiment, key phrases, topics and language" to correctly format your input rows in JSON as the request body and try again.
Hope it helps.
If you can update your question for specifying which ML REST API you used, I think it's very helpful for figuring out the issue.
Expect your update.

ERROR (100) : Parameters do not match any fields that can be updated

I am trying to post Facebook Page status as page(not user) through my script. This returns me an error 100.
Here I am generating temporary user_access_token from Graph Explorer with permission : manage_pages, publish_pages
Code:
<?php
$url='https://graph.facebook.com/v2.3/{$user_id}/accounts?access_token=USER_ACCESS_TOKEN';
$ch=curl_init();
CURL_SETOPT($ch,CURLOPT_URL,$url);
CURL_SETOPT($ch,CURLOPT_RETURNTRANSFER, 1);
$json=json_decode(curl_exec($ch));
$page_access_token=$json->data['0']->access_token;
curl_close($ch);
$page_id='xxx';
$message='helloworld';
$url="https://graph.facebook.com/v2.3/{$page_id}?access_token=$page_access_token";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $message);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
$result = json_decode(curl_exec($curl));
var_dump($result);
?>
If everything goes well, a string "helloworld" should get posted on Facebook page. But here it is returning with an error :
object(stdClass)#5 (1) {
["error"]=>
object(stdClass)#6 (3) {
["message"]=>
string(61) "(#100) Parameters do not match any fields that can be updated"
["type"]=>
string(14) "OAuthException"
["code"]=>
int(100)
}
}
What is mistake here ? Thank you.
You're trying to post to /<PAGE_ID>
The correct endpoint for creating a post on a Page is /<PAGE_ID>/feed, documented here: https://developers.facebook.com/docs/graph-api/reference/v2.3/page/feed
A valid format for a basic call to create a post would be https://graph.facebook.com/v2.3/<PAGE_ID>/feed?message=helloworld&access_token=<ACCESS_TOKEN>

Parse json answer from Redmine API with PHP

I'm trying to parse a JSON answer from Redmine API and I don't know how to get to the parts of the array.
Here is the code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'http://login:password#redmine.server/redmine/issues.json?cf_2=12345');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
$data = json_decode($response);
When I make a var_dump($data), the answer looks like this:
array(1) { [0]=> object(stdClass)#1853 (14) { ["id"]=> int(96) ["project"]=> object(stdClass)#1852 (2) { ["id"]=> int(68) ["name"]=> string(7) "Test.......
So, when I make a for loop, I would like to access the parts of the array:
foreach($data as $issues){
var_dump($issues["id"]);
}
And so on. Any idea on this?
Stupid me...
The culprit was here:
$data = json_decode($response);
Should be:
$data = json_decode($response,true);
Now I get a proper PHP array.

Json and printing the resulting array into variables

I am using cURL to get json information from a site that pulls a random tumblr picture from a list of sources and I am interested of putting the json data retrieved into php variables so I can call for example, just the image url
$url = "http://someurl.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url);
$a = curl_exec($ch);
$json = var_dump(json_decode($a, true));
I get:
array(3) {
'image' =>
array(1) {
[0] =>
string(62) "http://picture.jpg"
}
'source' =>
string(31) "http://source.tumblr.com"
'page' =>
string(9) "/page/164"
}
What would I now do in order to just print the url for the image?
I have tried
$url = $json["image"][0];
and then calling $url, but it gives me nothing in return. What I am doing wrong?
I have never worked with json before so I am at a loss here, any help is appreciated!
according to code looks:-
try
change
$json = var_dump(json_decode($a, true));
to
$json = json_decode($a, true);

Categories